Posted by: Anonymous
[ip: 12.2.142.7]
on November 06, 2007 04:10 PM
Perl's command line methods are similar to awk, but there are multiple options:
For example: you could use a substitution to remove all but the last element of the path:
pwd | perl -p -e 's/.*\/([^\/]+)$/$1/o'
In the above, '-p' tells per to read in all input lines one at a time to the $_ variable and then print the value of $_ when processing is done. The '-e' tells perl to execute the string that follows as a script to operate on $_. the 's/' starts the substition type regular expression which will operate on $_. The '.*\/' matches any characters up to and including a forward slash. '([^\/]+)' captures a string that does not contains any backslashes, and the '$' at the end ensures this captured string is at the end of the input line. The resulting $1 (the captured string) is then assigned as the new value of the implicit $_ variable which was passed to the substitution and will be printed out (since we specified '-p' at the command line).
Another way would be to use a split into an array and then print the last element of the array:
This can take two different forms:
pwd | perl -e '@a=split(/\//o,<>);print $a[-1];'
pwd | perl -p -e '@a=split(/\//o,$_);$_=$a[-1];'
In the first example, we omit '-p' and simply read in stdin directly via the '<>' operator, and this data is split by the '/\//o' regular expression (which matches on all backslashes) into an array '@a', and then the print command sends the last element of the array 'a[-1]' to stdout.
In the second example, we leverage the fact that 'split()' implicitly splits the input data in $_, and then we can simply reassign $_ to the last element in @a, which is $a[-1].
Number 5: Perl Way(s)
Posted by: Anonymous [ip: 12.2.142.7] on November 06, 2007 04:10 PMFor example: you could use a substitution to remove all but the last element of the path:
pwd | perl -p -e 's/.*\/([^\/]+)$/$1/o'
In the above, '-p' tells per to read in all input lines one at a time to the $_ variable and then print the value of $_ when processing is done. The '-e' tells perl to execute the string that follows as a script to operate on $_. the 's/' starts the substition type regular expression which will operate on $_. The '.*\/' matches any characters up to and including a forward slash. '([^\/]+)' captures a string that does not contains any backslashes, and the '$' at the end ensures this captured string is at the end of the input line. The resulting $1 (the captured string) is then assigned as the new value of the implicit $_ variable which was passed to the substitution and will be printed out (since we specified '-p' at the command line).
Another way would be to use a split into an array and then print the last element of the array:
This can take two different forms:
pwd | perl -e '@a=split(/\//o,<>);print $a[-1];'
pwd | perl -p -e '@a=split(/\//o,$_);$_=$a[-1];'
In the first example, we omit '-p' and simply read in stdin directly via the '<>' operator, and this data is split by the '/\//o' regular expression (which matches on all backslashes) into an array '@a', and then the print command sends the last element of the array 'a[-1]' to stdout.
In the second example, we leverage the fact that 'split()' implicitly splits the input data in $_, and then we can simply reassign $_ to the last element in @a, which is $a[-1].
#