Twitter

awk Tutorial -- 1 -- Concept and Syntax

This blog is part of an awk Tutorial, you'll find the whole list of section already covered here.
awk's concept is as below:
  • Read one or many files (or any input coming from a pipe) line by line; each line being contained in a variable named $0
  • Split each line in columns; the first column being stored in a variable named $1, the second column in a $2 variable, etc...
  • awk can also execute commands before and after reading an input

awk's syntax is as below (I have colored each section to make it clear):
Note that the whole awk command is between simple quotes and can be written on the same line:
awk 'BEGIN {} {} END {}' file1 file2
Let's look at a first example reading an /etc/passwd file:
$ awk 'BEGIN {print "Before reading the file"} {print "For each line " $0} END {print "After reading the file"}' /etc/passwd
Before reading the file                                <== BEGIN{}
For each line root:x:0:0:root:/root:/bin/bash          <== {}
For each line bin:x:1:1:bin:/bin:/sbin/nologin         <== {}
For each line daemon:x:2:2:daemon:/sbin:/sbin/nologin  <== {}
After reading the file                                 <== END{}
$
Note that each section is optional; one, two or three sections can be used as shown below:
$ awk 'BEGIN {print "Before reading the file"} END {print "After reading the file"}' /etc/passwd
Before reading the file                                <== BEGIN{}
After reading the file                                 <== END{}
$ awk '{print "For each line " $0}' /etc/passwd
For each line root:x:0:0:root:/root:/bin/bash
For each line bin:x:1:1:bin:/bin:/sbin/nologin
For each line daemon:x:2:2:daemon:/sbin:/sbin/nologin
$
A last thing we learnt from these examples is that we use print to print an output from awk and we could easily concatenate the string "For each line " and the whole lines "$0" just by doing:
print "For each line " $0

No comments:

Post a Comment

Some bash tips -- 18 -- paste

This blog is part of a shell tips list which are good to know -- the whole list can be found here. I really like finding a real usage for...