X

Alejandro Vargas' Blog

  • January 1, 2007

awk examples

Alejandro Vargas
Technical Leader, ACS Global Delivery, Infrastructure & BigData

awk is a most useful command in unix, here some simple sintax examples

testfile:
------------------ from next line --------------------
1 123 456

2 3 4 5 6 7

a ab ac adfg
------------------ to previous line ------------------

NF number of fields:

 awk '{c=NF; print c}' testfile
3
0
6
0
4

Count non blank lines:





awk 'NF != 0 {++count} END {print count}'  testfile


3

Filter out blank lines:

awk 'NF != 0 {print} ' testfile
1 123 456
2 3 4 5 6 7
a ab ac adfg

Print aditional line after each line:

awk '{print ; print ""}' testfile
1 123 456



2 3 4 5 6 7



a ab ac adfg

Print aditional line after each line, if line is not blank:

 awk '{print ; if (NF != 0) print ""}' testfile
1 123 456


2 3 4 5 6 7


a ab ac adfg

Count non blank lines:

awk 'NF != 0 {++count} END {print count}'  testfile
3

Split every string on a different line:

awk '{c=split($0, s); for( n=1; n<=c; ++n) print s[n] }' testfile
1
123
456
2
3
4
5
6
7
a
ab
ac
adfg

awk like grep:

awk '/5/' testfile
1 123 456
2 3 4 5 6 7

awk more than grep:

awk '{if ($3 > 100) print $3, " is bigger than 100"}' testfile
456  is bigger than 100
ac  is bigger than 100

Calculate the total of one field:

Taving several import log files I want to have the total number of rows imported, this is the line containing the total rows imported on different sessions:

> grep row *log
PART20050108_01.log:. . importing table             "XPARTITION"    4221874 rows imported
PART20050108_02.log:. . importing table             "XPARTITION"    4248632 rows imported
PART20050108_03.log:. . importing table             "XPARTITION"    1795215 rows imported

To get the total of field 6 that contain the number of rows imported I can do:

> grep row *log | awk '{sum +=$6} END {print sum}'
10265721

Print the fibonaci sequence:

awk 'BEGIN {a=1;b=1; while(++x<=10){print a; t=a;a=a+b;b=t}; exit}'
1
2
3
5
8
13
21
34
55
89


Join the discussion

Comments ( 2 )
  • Dimitre Radoulov Monday, January 1, 2007
    And because of some default actions
    it's possible to rewrite some of the examples like this:
    } NF number of fields:
    awk '{print NF}' testfile
    } Filter out blank lines:
    awk 'NF!=0' testfile
    } Print aditional line after each line, if line is not blank:
    awk '1;NF!=0{print ""}' testfile
    } Split every string on a different line:
    awk '{for (i=1;i<=NF;i++)print $i}' testfile
    Regards
    Dimitre
  • Ildiko Monday, February 23, 2009
    Hello,
    any idea why this command does not work on oracle 10?
    cat $ORACLE_HOME/dbs/init$ORACLE_SID.ora | grep log_archive_dest | awk ' {print $3} '
    Thx,
    Ildiko
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.