Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 5 Oct 2015 16:59:02 +0200
From:      Polytropon <freebsd@edvax.de>
To:        Quartz <quartz@sneakertech.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: awk question
Message-ID:  <20151005165902.ad01c288.freebsd@edvax.de>
In-Reply-To: <56124479.9020505@sneakertech.com>
References:  <5611C922.4050007@hiwaay.net> <20151005042129.1f153ec6.freebsd@edvax.de> <5611F776.9090701@hiwaay.net> <56124479.9020505@sneakertech.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 05 Oct 2015 05:35:53 -0400, Quartz wrote:
> > Funny you mention grep, I had a similar conversation on the NetBSD list
> > last week & everyone there suggested using awk alone to 'grep' out the
> > lines I wanted.
> 
> Awk, sed, and various other utilities all have basic regex line 
> matching, so if you're already calling them for some other feature then 
> piping the output through grep first is kinda silly and inefficient. 
> It's sorta like doing $(cat foo.txt | grep 'bar') instead of just $(grep 
> 'bar' foo.txt). I suggested it mainly because it's easy to understand 
> conceptually and because awk's syntax has something of a steep learning 
> curve.

The example "cat foo.txt | grep 'bar'" is a case of "useless use
of cat". However, it makes the processing order more clear. On
the other hand, the first output program might be a "real program"
(not just "cat" printing a file that alredy exists).

The form "input | step1 | step2 | step3 | step4 > result" usually
is more readable than what's possible with <, << EOF, <(), `cmd`
or $(cmd). But it depends on the actual problem you want to solve
which tool from the toolbox you will use.

Additionally, awk isn't that hard to learn. Reading "man awk" will
provide you with a good background. And if you're already a C
programmer, you'll see that many things you can do in C will also
work similarly in awk, which _might_ not even be a good thing. :-)

Simple tasks where inefficiency doesn't matter - go with grep,
tr, cut, sort, maybe sed. More complex things (like document
generators from database input) - consider awk.


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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20151005165902.ad01c288.freebsd>