lastnode

Table of Contents

Prototype: lastnode(string, separator)

Return type: string

Description: Returns the part of string after the last separator.

This function returns the final node in a chain, given a regular expression to split on. This is mainly useful for finding leaf-names of files, from a fully qualified path name.

Arguments:

  • string: string - Input string - in the range: .*
  • separator: string - Link separator, e.g. /,: - in the range: .*

Example:

body common control
{
      bundlesequence => { "yes" };
}

bundle agent yes
{
  vars:

      "path1" string => "/one/two/last1";
      "path2" string => "one:two:last2";
      "path4" string => "/one/two/";

      "last1" string => lastnode("$(path1)","/");
      "last2" string => lastnode("$(path2)",":");

      "last3" string => lastnode("$(path2)","/");
      "last4" string => lastnode("$(path4)","/");

  reports:
      "Last / node in / path '$(path1)' = '$(last1)'";
      "Last : node in : path '$(path2)' = '$(last2)'";
      "Last / node in : path '$(path2)' = '$(last3)'";
      "Last / node in /-terminated path '$(path4)' = '$(last4)'";

}

Output:

R: Last / node in / path '/one/two/last1' = 'last1'
R: Last : node in : path 'one:two:last2' = 'last2'
R: Last / node in : path 'one:two:last2' = 'one:two:last2'
R: Last / node in /-terminated path '/one/two/' = ''

See also: filestat(), dirname(), splitstring().