Exercise #1: Learning awk Basics(bashshell.net)
bashshell.net
Exercise #1: Learning awk Basics
http://bashshell.net/stream-filtering-utilities/exercise-1-learning-awk-basics/
4 comments
Actually, all three authors of that book (not just BWK) are the original authors of awk(1).
Correct, didn't think that sounded ambiguous.
FYI, the bell-labs link seems to be broken.
The ISBN is 020107981X.
For some reason, new copies are really expensive ($80+), but used copies are typically only $15ish.
For some reason, new copies are really expensive ($80+), but used copies are typically only $15ish.
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.
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}'
Does anyone know how to achieve the following command in a more type friendly way?:
awk '{print $1}'
Actually the single quotes are not part of the Awk language. They are used by the posix shell to preservers the literal meaning of the characters, in this case { } and $. they are not needed in self contained Awk scripts.
I see there's a bunch of replies, some with more and more commands through a pipe.
Seriously, if you are going to be using a shell, you will be typing a ' char all the time, so probably better to just get used to it rather than go through all those convolutions with tr and cut.
Seriously, if you are going to be using a shell, you will be typing a ' char all the time, so probably better to just get used to it rather than go through all those convolutions with tr and cut.
cut -f1
Not working for me...
> echo '1 2 3 4' | awk '{print $1}'
1
> echo '1 2 3 4' | cut -f1
1 2 3 4
> echo '1 2 3 4' | awk '{print $1}'
1
> echo '1 2 3 4' | cut -f1
1 2 3 4
echo '1 2 3 4' | cut -d' ' -f1
By default cut uses TAB as delimiterNot 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.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.[deleted]
Ok.
> echo '1 2 3 4' | sed 's/ */\t/g' | cut -f2
2
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.