shell script

Jonathan S. Billings jsbillin at Princeton.EDU
Wed Jul 18 12:56:11 UTC 2007


Mahalingam Subramaniam wrote:
> 
> 
> cud u plz. explain the proces involved in this perl script comprehensively.

Sure, it's actually quite simple:

I run 'perl -nale'  this means that:
1.) -n means that it will run through every line in the input file, but 
not print it.
2.) -a means autosplit.  This automatically breaks up each line into an 
array @F.  It removes the whitespace.
3.) -l means every time you print something, it puts a newline at the 
end of the line.
4.) -e means 'execute the following as a perl script'

Then, I have the short oneliner:
  'print((split("/",$F[1]))[-1]) if $F[0]=~/^\d+G/'

This code, when written in english, does:
Print the last element in the path (the second object in the input) if 
the first object of the input is a number (or several numbers) followed 
by 'G').
I chose perl because there were a couple situations I wanted to account 
for, and perl handled them better.  For example, is there whitespace 
before the size?  Is the username always the 4th item in the path?



I suppose, if you really wanted to use awk, you could do something like 
this:

awk '/^[0-9 \t]*G/ {print $2}' mailstorage.txt | xargs -n 1 basename
(notice I include a space and a tab in the awk search pattern, just in 
case your file has whitespace at the beginning of each line).

'xargs -n 1' will pass every line in the output of the awk statement to 
'basename', which will strip off the leading components of a path.

-- 
Jonathan Billings <jsbillin at princeton.edu>
Computational Science and Engineering Support (CSES)
http://www.princeton.edu/~cses/




More information about the redhat-sysadmin-list mailing list