awk question

Polytropon freebsd at edvax.de
Mon Oct 5 14:54:20 UTC 2015


On Sun, 4 Oct 2015 22:14:02 -0700, HM Edwards wrote:
> 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.

Well, I've been abusing "grep | tr" and "grep | sed" for a long
time inside shell scripts before I recognized that awk can do
this - but it's only a case for awk when it's actually required
to postprocess and re-arrange the filtering results, as I concluded
from the desired output example provided. Otherwise, yeah, I'd
simply go with grep if the modification is not that complicated.
There are many tools in our toolbox. :-)




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


More information about the freebsd-questions mailing list