Command Line Examples: gawk
From PHYSpedia
gawk
is a powerful stream editor that can be used in command pipelines to perform special functions that do not have a command written for. The easiest use of gawk (and a very common one) is to manipulate the columns in text stream.
In the examples that follow, assume that a file name test-data.txt
exists and contains 3 columns of data.
user@host: $ cat test-data.txt
1 11 21
2 12 22
3 13 23
4 14 24
Print only columns 1 and 2 of the datafile
user@host: $ cat test-data.txt| gawk '{print $1,$2}'
1 11
2 12
3 13
4 14
Print only columns 1 and 3 of the datafile
user@host: $ cat test-data.txt| gawk '{print $1,$3}'
1 21
2 22
3 23
4 24
Print columns in reverse order
user@host: $ cat test-data.txt| gawk '{print $3,$2,$1}'
21 11 1
22 12 2
23 13 3
24 14 4
Print all columns, but multiply column 2 by 100
user@host: $ cat test-data.txt| gawk '{print $1,100*$2,$3}'
1 1100 21
2 1200 22
3 1300 23
4 1400 24
Print column 1 and column 3 squared
user@host: $ cat test-data.txt| gawk '{print $1,$3**2}'
1 441
2 484
3 529
4 576
Print column 1 and sine of column 1
user@host: $ cat test-data.txt| gawk '{print $1,sin($1)}'
1 0.841471
2 0.909297
3 0.14112
4 -0.756802
Print columns 1 and 3 for all but the first two lines
user@host: $ cat test-data.txt| gawk 'NR>2{print $1,$3}' 3 23 4 24
Print columns 1 and 3 times some multiplication factor passed in as a varible
user@host: $ scalefactor=4 user@host: $ cat test-data.txt| gawk -v scale=${scalefactor} '{print $1,scale*$3}' 1 84 2 88 3 92 4 96