Difference between revisions of "Command Line Examples: gawk"
From PHYSpedia
| Line 16: | Line 16: | ||
3 13 | 3 13 | ||
4 14 | 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 | ||
Revision as of 13:31, 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