Difference between revisions of "Command Line Examples: gawk"

From PHYSpedia
Jump to: navigation, search
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 16: Line 18:
 
  3 13
 
  3 13
 
  4 14
 
  4 14
 +
</code>
  
  
 
Print only columns 1 and 3 of the datafile
 
Print only columns 1 and 3 of the datafile
 +
<code>
 
  user@host: $ cat test-data.txt| gawk '{print $1,$3}'
 
  user@host: $ cat test-data.txt| gawk '{print $1,$3}'
 
  1 21
 
  1 21
Line 24: Line 28:
 
  3 23
 
  3 23
 
  4 24
 
  4 24
 +
</code>
  
  
 
Print columns in reverse order
 
Print columns in reverse order
 +
<code>
 
  user@host: $ cat test-data.txt| gawk '{print $3,$2,$1}'
 
  user@host: $ cat test-data.txt| gawk '{print $3,$2,$1}'
 
  21 11 1
 
  21 11 1
Line 32: Line 38:
 
  23 13 3
 
  23 13 3
 
  24 14 4
 
  24 14 4
 +
</code>
  
  
 
Print all columns, but multiply column 2 by 100
 
Print all columns, but multiply column 2 by 100
 +
<code>
 
  user@host: $ cat test-data.txt| gawk '{print $1,100*$2,$3}'
 
  user@host: $ cat test-data.txt| gawk '{print $1,100*$2,$3}'
 
  1 1100 21
 
  1 1100 21
Line 40: Line 48:
 
  3 1300 23
 
  3 1300 23
 
  4 1400 24
 
  4 1400 24
 +
</code>
  
  
 
Print column 1 and column 3 squared
 
Print column 1 and column 3 squared
 +
<code>
 
  user@host: $ cat test-data.txt| gawk '{print $1,$3**2}'     
 
  user@host: $ cat test-data.txt| gawk '{print $1,$3**2}'     
 
  1 441
 
  1 441
Line 48: Line 58:
 
  3 529
 
  3 529
 
  4 576
 
  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>

Revision as of 13:32, 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