Date: Sun, 4 Oct 2015 22:14:02 -0700 From: HM Edwards <hedwards816@gmail.com> To: freebsd-questions@freebsd.org Subject: Re: awk question Message-ID: <5612071A.4090001@gmail.com> In-Reply-To: <20151005060351.3646d1b7.freebsd@edvax.de> References: <5611C922.4050007@hiwaay.net> <5611EEE2.9030100@sneakertech.com> <20151005060351.3646d1b7.freebsd@edvax.de>
next in thread | previous in thread | raw e-mail | index | archive | help
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.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5612071A.4090001>