AWK Commands in Linux II


AWK commands in Linux

AWK programming has various built in commands which enables scanning every record and field inside a file. However, one must be careful to use as Linux has eyes that can look spaces too. Basically AWK built in commands has defined values like field separator, record separator etc.

AWK FS command

The FS command acts as a separator in the fields. Any character can be provided to FS which is used as the delimiter. The delimiter can be a single character or a combination of single characters to make it as a word.
For example, if we have a word “DICTATORSHIP” and the FS=”T” then the output will be
DIC A ORSHIP
T acts as a separator or better say field separator.

The value of FS can be set in the BEGIN clause or using –F command line.
Also, we have NR command which counts the records we have passed in the file. NR can be used in the BEGIN clause or even in the END clause.

Here is an example of AWK FS as well as NR commands

Awk ‘BEGIN {FS = “O”;}
{ Print $1”\t”$2”\t”$3”\t”$4”;}
END {print NR, “ records have been passed”;}’ test.txt

Another example, but this time FS is not a character, but a word.

Awk ‘BEGIN {FS = “CEO”;}
{ Print $1”\t”$2”\t”$3”\t”$4”;}
END {print NR, “ records have been passed”;}’ test.txt

The output of both the above codes.


AWK OFS COMMAND

OFS stands for Output Field Separator. OFS is similar to AWK FS command. The default value of OFS command is a space. The AWK OFS appears in between the fields.


Here is an example with AWK OFS

Awk ‘BEGIN{ OFS = “CEO” ;}
{ Print $2,$4; }
END { print NR, “ records have been passed”; }’ test.txt

Example 2 

Awk ‘BEGIN{ OFS = “<here lies OFS>” ;}
{ Print $2,$4; }
END { print NR, “ records have been passed”; }’ test.txt


Here is the output of the above code.


Note- Remember that while editing the data in VI or VIM editor a line is also considered as a record. I was getting 6 records have been passed in output.

One can easily decipher here that AWK FS command decides the separation character or word. That letter or word is treated as space an the output is thrown at stdout. However the AWK OFS command decides with which character will two fields will be separated. Default its space however we changed it in previous code.

Here is the output 
(OFS is basically a character or word with which concatenation is done)

AWK ORS Command

ORS stands for Output Record Separator. Its operation is very simple. Each record will be separated with this delimiter.

An example for AWK ORS

Awk ‘BEGIN{ ORS = “\n@\n” ;}

{ Print; }


END { print NR, “ records have been passed”; }’ test.txt

AWK NR Command

NR stands for number of records preocessed variable. It even counts a blank line as a record so one has to be careful. Well we already covered it in most other above examples in the END clause. It can be used in the coding section and will be evaluated to the records that has passed each time.

AWK NF Command

NF stands for number of fields. It counts the number of fields in outputs them at stdout. 
Example
awk '{ print "Record Number ",NR," has ",NF," fields";}' test.txt

AWK FNR Command

FNR stands for File Number of Records. It will give the number of records in each file.

Example 
awk '{print FILENAME, FNR;} test.txt test2.txt

Output for ORS and FNR along with NR Command

The difference between FNR and NR is in the below image. Enough for the geeks out there to understand.



AWK If-Else Commands  will be updated soon.

No comments:

Post a Comment