Difference between revisions of "Command Line Examples: gawk"
From PHYSpedia
(3 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
In the examples that follow, assume that a file name <code>test-data.txt</code> exists and contains 3 columns of data. | In the examples that follow, assume that a file name <code>test-data.txt</code> exists and contains 3 columns of data. | ||
− | + | <code> | |
user@host: $ cat test-data.txt | user@host: $ cat test-data.txt | ||
1 11 21 | 1 11 21 | ||
Line 8: | Line 8: | ||
3 13 23 | 3 13 23 | ||
4 14 24 | 4 14 24 | ||
+ | </code> | ||
+ | |||
Print only columns 1 and 2 of the datafile | Print only columns 1 and 2 of the datafile | ||
+ | <code> | ||
user@host: $ cat test-data.txt| gawk '{print $1,$2}' | user@host: $ cat test-data.txt| gawk '{print $1,$2}' | ||
1 11 | 1 11 | ||
Line 15: | Line 18: | ||
3 13 | 3 13 | ||
4 14 | 4 14 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Print only columns 1 and 3 of the datafile | ||
+ | <code> | ||
+ | user@host: $ cat test-data.txt| gawk '{print $1,$3}' | ||
+ | 1 21 | ||
+ | 2 22 | ||
+ | 3 23 | ||
+ | 4 24 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Print columns in reverse order | ||
+ | <code> | ||
+ | user@host: $ cat test-data.txt| gawk '{print $3,$2,$1}' | ||
+ | 21 11 1 | ||
+ | 22 12 2 | ||
+ | 23 13 3 | ||
+ | 24 14 4 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Print all columns, but multiply column 2 by 100 | ||
+ | <code> | ||
+ | user@host: $ cat test-data.txt| gawk '{print $1,100*$2,$3}' | ||
+ | 1 1100 21 | ||
+ | 2 1200 22 | ||
+ | 3 1300 23 | ||
+ | 4 1400 24 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Print column 1 and column 3 squared | ||
+ | <code> | ||
+ | user@host: $ cat test-data.txt| gawk '{print $1,$3**2}' | ||
+ | 1 441 | ||
+ | 2 484 | ||
+ | 3 529 | ||
+ | 4 576 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | Print column 1 and sine of column 1 | ||
+ | <code> | ||
+ | user@host: $ cat test-data.txt| gawk '{print $1,sin($1)}' | ||
+ | 1 0.841471 | ||
+ | 2 0.909297 | ||
+ | 3 0.14112 | ||
+ | 4 -0.756802 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | 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 |
Latest revision as of 12:35, 20 February 2013
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