[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Sheflug] Using awk



And Lo! The Great Prophet James Wallbank uttered these words of wisdom:
> Hi Imran, Hi Jonathan,
> 
> I too am interested in the /%/ construct. You didn't miss it in the 
> manual page, Imran, it isn't mentioned.
> 
> I see that % can be a format control letter, but in between slashes it 
> would appear to be a pattern.
> 
> I *guess* that it simply finds everything - but I can't prove it. I 
> tried Googling and Asking Jeeves but was unable to find a definitive 
> explanation. The issue with awk isn't that there isn't enough 
> documentation, there's much, much too much!

Nope: /%/ is a regular expression, in the same way that /A/ is, /^Hello/ is 
and /^.*\(hello[ 	]* [Ww]orld!*$/

% isn't a control character, it's just that - a percent sign - this rule is 
just matching lines that contain a %. Its the equivalent to /^.*%.*$/, but 
a little simpler ... :-)

The slashes that surround it is the way that awk identifies a regular 
expression, rather than, say, an explicit string, thus:

	"%" is a string containing a %
and
	/%/ is a regular expression looking for a %

Awk is rather powerful and more useful in many cases than perl (quicker to 
knock a quick script up). It's a rule based language - you define a rule, 
then awk executes instructions if that rule matches. In Jonathan's case, 
the simple:

	df | awk /%/ | sort

is using a shortcut for awk. A full spec for the rule would be:

	/%/ { print $0 }

...i.e., compare the line to the given regular expression and if it 
matches, print the entire line. And is essentially equiavlent to:

	df | grep '%' | sort

HTH :-)

Chris...

-- 
\ Chris Johnson                 \ NP: high wycombe - no escape [i.n past] (
 \ cej [at] nightwolf.org.uk          \ DigitalGunfire.com - [Industrial:EBM:Futu 
  \ http://cej.nightwolf.org.uk/  \ repop] Long Range, Hard Hitting!)
   \ http://redclaw.org.uk/        ~---------------------------------------


___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.