-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAWK.sh
154 lines (85 loc) · 2.34 KB
/
AWK.sh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'''
Awk is a scripting language used for manipulating data and generating reports.The awk command programming
language requires no compiling, and allows the user to use variables, numeric functions, string
functions, and logical operators.
Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements
that define text patterns that are to be searched for in each line of a document and the action that
is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing.
It searches one or more files to see if they contain lines that matches with the specified patterns and
then performs the associated actions.
'''
awk options 'selection _criteria {action }' input-file > output-file
'''
# Different data fields can be accessed separately:
$ awk '{print $1}' myfile
FIELDWIDTHS Specifies the field width.
RS Specifies the record separator.
FS Specifies the field separator.
OFS Specifies the Output separator.
ORS Specifies the Output separator.
ARGC Retrieves the number of passed parameters.
ARGV Retrieves the command line parameters.
ENVIRON Array of the shell environment variables and corresponding values.
FILENAME The file name that is processed by awk.
NF Fields count of the line being processed.
NR Retrieves total count of processed records.
FNR The record which is processed.
IGNORECASE To ignore the character case.
'''
# -------------------------------------------------------------------------------------------
$ awk '
BEGIN{
test="Welcome to LikeGeeks website"
print test
}'
# -----------------------------------------------------------------------------------------
$ awk '{if ($1 > 30) print $1}' testfile
#
$ awk '{
if ($1 > 30)
{
x = $1 * 3
print x
} else
{
x = $1 / 2
print x
}}' testfile
#
$ awk '{
sum = 0
i = 1
while (i < 5)
{
sum += $i
i++
}
average = sum / 3
print "Average:",average
}' testfile
#
$ awk '{
tot = 0
i = 1
while (i < 5)
{
tot += $i
if (i == 3)
break
i++
}
average = tot / 3
print "Average is:",average
}' testfile
#
$ awk '{
total = 0
for (var = 1; var < 5; var++)
{
total += $var
}
avg = total / 3
print "Average:",avg
}' testfile
#
# There are other in-built functions which can be used along with user defined functions like mathematical functions.