awk

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$ echo 'foo bar' | awk '{ print }'
foo bar

$ echo 'foo bar' | awk '{ print $0 }'
foo bar

$ echo 'foo bar' | awk '{ print $1 }'
foo

$ echo 'foo bar' | awk '{ print $2 }'
foo

$ echo 'foo
bar' | awk '{ print $1 }'
foo
bar

$ # concat using commas to concat with spaces
$ echo '1 2 3
4 5 6' | awk '{ print $1,$2 }'
1 2
4 5

$ # concat with content
$ echo '1 2 3
4 5 6' | awk '{ print $1 "," $2 }'
1,2
4,5

$ # NF == number of fields == last column
$ echo '1 2 3
4 5 6' \
| awk '{ print $NF }'
3
6

$ # -F: flag to set the field separator, default space
$ echo 'foo bar,popo tutu' | awk -F',' '{ print $2 }'
popo tutu

$ # working
$ echo 'id,first name,last name
1,foo,bar
2,Patrick,Dupont
3,Chuck,Noris' | awk -F',' '{ print $NF "\t" $(NF-1) }'
last name       first name
bar     foo
Dupont  Patrick
Noris   Chuck

$ # NR == number of rows
$ echo 'foo bar
one two' | awk '{ print NR " " $2 }'
1 bar
2 two

$ # regexp
$ echo 'foo poo
foobar hello' | awk '/bar/ { print $2 }'
hello

$ # match field
$ echo 'foo bar
popo titi' | awk '$2 == "titi" { print $1 }'
popo

$ # match within specific field
$ echo 'foobar popo
titi tutu' | awk '$1~/ba/ { print $2 }'
popo

$ # printf with substr (index starts from 1)
$ echo 'somelongstring
anotherlongstring' | awk '{ printf "%s\n", substr($1,1,5) }'
somel
anoth

$ # using %-Ns to set the desired column with
$ echo 'id,first name,last name
1,foo,bar
2,Patrick,Dupont
3,Chuck,Noris' | awk -F',' '{ printf "%-20s \t %-10s \t %s \n", NR, $2, $3 }'
1                        first name      last name
2                        foo             bar
3                        Patrick         Dupont
4                        Chuck           Noris

$ # AWK BEGIN END
$ echo '1,foo
2,bar
3,foobar' | awk -F',' '
BEGIN { print "computing average..." }
      { total = total + $1 }
END   { print "average is:", total/NR }'
computing average...
average is: 2