-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter Commands: basic awk command - syntax | options | variables
More file actions
59 lines (42 loc) · 2.74 KB
/
Copy pathFilter Commands: basic awk command - syntax | options | variables
File metadata and controls
59 lines (42 loc) · 2.74 KB
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
> Basic awk command
- awk command is a powerful method for processing or analyzing text or data files, which are organized by lines (rows or records) and columns (fields)
- We can use awk as a linux command and also as a Scripting language like Bash Shell Scripting (Advance)
SYNTAX --
>> awk [options] '[selection_criteria] {action}' input_file
- options - are always optional
- selection_criteria - can be condition, pattern/search string and also can be optional
- action - logic to perform on each row/record (print (mostly used))
>> cat input_file | awk [options] '[selection_criteria] {action}'
- mostly used in CLI
OPTIONS --
-F : fs : To specify a Field Separator (default field separator in awk is space (tab is nothing but multiple spaces))
-f : file : To specify a file that contains awk script
-v : var=value : To declare a Variable
SIMPLE awk command SYNTAX --
>> awk '{action}' input_file
- eg. awk {print $1} file.txt # '$1' prints first field from each line
- Default Variables for awk
$0 : Entire File
>> awk '{print $0}' file.txt # prints entire file content
>> awk '{print}' file.txt # also prints entire file content
>> awk '{print $1,$3}' file.txt # 1st and 3rd columns/fields of each line/row/record. Output is by default separated by ' '.
>> awk 'BEGIN {OFS=":"} {print $1,$3}' file.txt # output is separated by ':'.
>> awk '{print $3,$1}' file.txt # reverses order of printing 1st and 3rd column to 3rd and 1st column
>> awk '{print NR}' file.txt # prints only Line(Record) Number on each line
>> awk '{print NR,$0}' file.txt # prints Line Number on each line along with their content
>> awk '{print NR,$0,NF}' file.txt # prints Line Number alongwith their content(all fields) and in the end Number of Fields present in that record
>> awk '{print $NF}' file.txt # prints last field of each row
>> awk '{print $NR}' file.txt # prints 1st field from 1st record, 2nd field from 2nd record, 3rd from 3rd and so on.
- grep with cut vs awk
>> apache2 -v | grep "version" | cut -d '/' -f 2 | cut -d ' ' -f 1
2.4.54
>> apache2 -v | awk -F '[ /]' '/version/{print $4}' # ' ' & '/' are separators inside [], '/version/' is search string (field) & 'print $4' inside {} is an action.
2.4.54
# both the commands give same output
>> apache2 -v | awk -F '[ /]' 'NR==1{print $4}' # NR is a condition, i.e. Record Number == 1, record is line and the condition means 'in line No. 1'
2.4.54
ADVANTAGE --
- Single awk command works instead of using grep alongwith cut, especially for fields and patterns.
LIMITATION --
- In awk command, we will get only the fields and not the characters. For characters only cut command works.
** ADVANCED AWK COMMAND FOR SHELL SCRIPTING WILL BE DISCUSSED LATER **