awk question

Polytropon freebsd at edvax.de
Mon Oct 5 04:03:54 UTC 2015


On Sun, 04 Oct 2015 23:30:42 -0400, Quartz wrote:
> Considering you're referring to 'pulling out lines', maybe you want to 
> trap the output of smartctl into a variable, then just echo that to grep 
> a bunch of times, before awk?
> 
> ie;
> 
> x=$(smartctl)
> 
> echo "$x" | grep 'foo' | awk '{print $1}'
> echo "$x" | grep 'bar' | awk '{print $2}'
> echo "$x" | grep 'baz' | awk '{print $3}'
> 
> ...etc?

Note that awk has "builtin grep", so your example could be
combined to one smartctl call:

	smartctl | awk '
		/foo/ { print $1; }
		/bar/ { print $2; }
		/baz/ { print $3; }
	'

Of course storing the smartctl output to a variable is very
useful when processing it _multiple_ times. But as you said,
awk is quite versatile. :-)



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list