cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Starfighter Starfighter
Starfighter
  • 21 Views

awk Tip - #3

$  awk -F'\t' 'BEGIN {print "Age\t Last,First,Mid\n"} {print $4"\t",$3",",$1","$2}' filename

In the above command, there are two actions to be performed - each action is
enclosed in a paif of braces, { action }.   The keyword, BEGIN, is associated
with ONLY the first action.

The BEGIN keyword specifies actions to be taken before any lines are read by
awk.  So, the first action block will be executed without awk reading any input,
and that action will be performed only once.

Some of the uses of the BEGIN keyword are to perform actions such as initialzing
variables, or printing headers - printing headers is what I'm using it for in the
above example.

Give the above command a try on a file that contains the following content:

John(tab)R(tab)Ripper(tab)88
Mary(tab)L(tab)Lamb(tab)55
Sally(tab)S(tab)Walker(tab)71
Peter(tab)P(tab)Piper(tab)39

 

Trevor "Red Hat Evangelist" Chandler
Labels (3)
0 Kudos
0 Replies
Join the discussion
You must log in to join this conversation.