Difference between revisions of "Command Line Examples: gawk"

From PHYSpedia
Jump to: navigation, search
Line 3: Line 3:
 
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.
  
  eigen % cat test-data.txt
+
  user@host: $ cat test-data.txt
 
  1  11  21
 
  1  11  21
 
  2  12  22
 
  2  12  22
 
  3  13  23
 
  3  13  23
 
  4  14  24
 
  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

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