awk examples

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


Comments:

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

Posted by Dimitre Radoulov on January 01, 2007 at 11:33 AM IST #

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

Posted by Ildiko on February 23, 2009 at 05:58 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

bocadmin_ww

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today