

Exercise #1: Learning awk Basics - aweber
http://bashshell.net/stream-filtering-utilities/exercise-1-learning-awk-basics/

======
silentbicycle
The post doesn't mention it, but the best source for learning awk is _The Awk
Programming Language_ (<http://cm.bell-labs.com/cm/cs/awkbook/>) by Aho,
Weinberger, and Kernighan. It's short and to the point, has a good tutorial
and reference, and it's even co-written by Brian Kernighan. It's _that_ good.
There's also a collection at resources at <http://awk.info/> .

Awk's pattern-structured rules are very powerful, and learning to use them
well (rather than writing if statements, etc.) is a big part of using awk
idiomatically.

Also, note which awk you're using - awk may actually be nawk ("new awk", the
awk that book covers) or GNU awk, which, in typical FSF fashion, has grafted
on a whole bunch of extra functionality. (Although the networking stuff is
cool.) Things will probably make more sense if you start with nawk, and _then_
explore the sprawling extensions of gawk.

~~~
koenigdavidmj
Actually, all three authors of that book (not just BWK) are the original
authors of awk(1).

~~~
silentbicycle
Correct, didn't think that sounded ambiguous.

------
bpodgursky
Awk is one of those tools I didn't know about until 6 months ago, and now I
have no idea how I lived without it for so long. If you're manipulating text
files, it's just so effortless.

------
aweber
This is the first of a 10 days series...

~~~
somabc
Thanks for this I look forward to the rest.

------
johnrob
I really wish awk were less awkward to type (no pun intended). The single
quotes plus the brackets is a brutal combo.

Does anyone know how to achieve the following command in a more type friendly
way?:

awk '{print $1}'

~~~
robertduncan
cut -f1

~~~
johnrob
Not working for me...

> echo '1 2 3 4' | awk '{print $1}'

1

> echo '1 2 3 4' | cut -f1

1 2 3 4

~~~
pmarin

       echo '1 2 3 4' | cut -d' ' -f1
    

By default cut uses TAB as delimiter

~~~
nimrody
Not to be picky, but still not good enough:

    
    
       echo '1    2     3 4' | cut -d' ' -f2
    

'cut' fails when fields are separated by multiple spaces.

~~~
molinari
I usually pipe through a whitespace-to-single-tab command. So

    
    
      echo '1   2  3 4' | sp2tab | cut -f2
    

would be equivalent to

    
    
      echo '1   2  3 4' | awk '{print $2}'
    

but quicker to write.

