awk question

HM Edwards hedwards816 at gmail.com
Mon Oct 5 05:14:58 UTC 2015


On 10/04/15 21:03, Polytropon wrote:
> 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. :-)
>
>
>
Hello,
If it were me, I'd probably just grep for the bits of output I'm looking 
for, pipe it to tr to remove the new lines, then have awk parse out the 
single line into the output I'm looking for.

As in something like
smartctl -l scttemp /dev/ada0 | grep '(foo|bar|foo2|bar2)' | tr -d "\n" 
| awk '{print "label 1 " $1 "label 2 " $2}'

It's another possibility there, although, it does make somewhat less use 
of awk.  You could probably also remove the grep completely and just use 
awk to spit out the lines you're interested in. I'm just a habitual grep 
abuser.

		





More information about the freebsd-questions mailing list