Code Scrappers


AWK - An Adwanced Filter


: awk option ‘selection_criteria {action}’ file(s)

~ To display containing a specific word.

saarthak@DESKTOP-N01EJET:~$ awk '/director/{print}' file1.c
02 | Saarthak Moghe |  director | sales | 8000
03 |  Ritika Sinha |director |design|5500
04 |  Rishabh Saxena| director|logistics |9000
05 |  Raj saksena |director | advertisement |10000
              



:awk option ‘selection_criteria {action}’ file(s) -

~ Multiple patterns can be given using ‘|’.

saarthak@DESKTOP-N01EJET:~$ awk '/sales|manager/{print}' file1.c
02 | Saarthak Moghe |  director | sales | 8000
06 | Rishav Raj     |  director | sales|8900
07 | Barun Sharma   |  manager  | sales |10000
              



: awk option ‘selection_criteria {action}’ file(s) -

~ Multiple patterns can be given using ‘|’ and $0 indicates the entire line.

saarthak@DESKTOP-N01EJET:~$ awk '/sales|manager/{print $0}' file1.c
02 | Saarthak Moghe |  director | sales | 8000
06 | Rishav Raj     |  director | sales|8900
07 | Barun Sharma   |  manager  | sales |10000
              



: Pattern Matching with AWK -

~ Awk uses regular expression for pattern matching.

saarthak@DESKTOP-N01EJET:~$ awk -F "|" '/sa[kx]s*ena/' file1.c
04 | Rishabh saxsena |  director|  logistics |9000
05 | Raj saksena    |  director | advertisement |10000
              



: Splitting a line into fields -

~ Awk uses special parameter, $0, to indicate entire line. It also uses $1, $2, $3 to identify fields.

saarthak@DESKTOP-N01EJET:~$ awk -F "|" '/sales/{ print $2,$4}' file1.c
Saarthak Moghe   sales
Rishav Raj       sales
Barun Sharma     sales
              



: NR -

~ If you want to select lines 3 to 6, all you have to do is use the built-in variable NR to specify the line numbers. .

saarthak@DESKTOP-N01EJET:~$ awk -F "|" 'NR==3,NR==6{ print NR, $2,$4}' file1.c
3  Rishabh saxsena    logistics
4  Raj saksena      advertisement
5  Rishav Raj       sales
6  Barun Sharma     sales