awk Example

awk Example

AWK :-AWK is a scripting language used for manipulating data and generating reports.

AWK operations:-
    1) scans a file line by line
    2) splits each input line into fields
    3) compares input line/fields to pattern
    4) performs action(s) on matched lines

AWK is used for :-
    1) Transform data files
    2) Produce formatted reports
    3) Programming constructs:
    4) Format output lines
    5) Arithmetic and string operations
    6) Conditionals and loops

Syntax :-

awk [options] ‘script’ file(s)

awk [options] –f scriptfile file(s)

Options :- There are 2 Options
        1) -f :- It is used for scripting file
        2) -F :- It is used for field Seprator

Buffers:- There are 2 buffers
    1) Record buffer :- $0 denote record
    2) Field buffer :- $1,$2,$3.. denotes filed1,field2,filed3..

Note:-awk mainly work on "pattern {action}" if pattern is missing, action is applied to all lines and if action is missing, the matched line is printed and must have either pattern or action.

AWK Variable :-
FS  :- Field separator (default=whitespace)
RS  :- Record separator (default=\n)
NF  :- Number of fields in current record
NR  :- Number of the current record
OFS :- Output field separator (default=space)
ORS :- Output record separator (default=\n)

Create a below file for practice of examples
cat>test.csv
Transaction ID,Number From,Number To,Donor,Port Request Time,Port Completion Time
20170404154326ANG2034,2349035331245,2349035331245,MTN,2017-04-04,2017-04-05
20170403085533ANG0915,2348099913646,2348099913646,ETS,2017-04-03,2017-04-05
20170404135527ANG1905,2348091018249,2348091018249,ETS,2017-04-04,2017-04-04
20170404135536ANG1906,2348092168403,2348092168403,ETS,2017-04-04,2017-04-04
20170404143043ANG1961,2348033670592,2348033670592,ETS,2017-04-04,2017-04-04
20170408142252ANG5326,2348037940394,2348037940394,MTN,2017-04-08,2017-04-08
20170404160152ANG2057,2348181761870,2348181761870,ETS,2017-04-04,2017-04-04
20170411151327ANG7109,2348055317575,2348055317575,GLO,2017-04-11,2017-04-12
20170411151534ANG7111,2348103570062,2348103570062,MTN,2017-04-11,2017-04-12 


Examples:-1) prints all lines containing string "MTN" in testfile

awk '/MTN/' test.csv

2) prints all lines with line number

awk '{ print NR,$0 }' test.csv

3) prints all lines and only 2,4 and 5th fields

awk -F, '{ print $2,$4,$5 }' test.csv

Expression Operators:- Following Operator works in Expression for AWK to pattern
 
1) Arithmetic Operator
2) Relational Operator
3) Logical Operator

1) Arithmetic Operator :-
+  Example:- $2+$3
-   Example:- $2-$3
*   Example:- $2*$3
/    Example:- $2/$3

2) Relational Operator :-
  <        Example:-   x < y
  < =    Example:-   x < = y
  ==     Example:-   x == y
  !=       Example:-   x != y
  >        Example:-   x > y
  > =    Example:-   x > = y
  ~        (Matched Regular Expression)  Example:-   x ~ /y/
  !~       (Not Matched Regular Expression)  Example:-    x !~ /y/
 2) Logical Operator :-
&&   Example:-  $2>100 && $5<500 br=""> ||      Example:-  $2>100 || $5 ~ /CLERK/

No comments:

Post a Comment