AWK With Examples 4


Example 13: Accessing individual fields in the Record.

We can access individual fields by making use of $ followed by field number.

By default fields are assumed to be separated by whitespaces(tabs and spaces).
We can change this by change this by changing the value of built in variable FS(field separator).

Notice the comma used to separate each field when we print.
If comma is not used the two fields will be concatenated

bos90631:sm017r awk '{print $2,$1}'  test1
8149158828 sukul
8149122222 uma
8097123451 bhanu
7798977047 shushant
9090909090 himanshu

Above program prints 2nd and 1st field in the  file(assuming space/tab as delimiter)

Example 14: Built in variable NF(number of fields)

NF: Built in variable NF contains the number of fields in a record.

We can also refer fields beyond the last one, but we will get only null string.
$0 represents entire record . Note that refering any field beyond the last field
does not give any error.


awk '{ print NF, $8}' test1
4
4
4
4
4


This prints the number of records in each  record of the file test1.


Example 15: Using non-numeric Constant with $

It is not compulsory to use a numeric constant with $ to print a field.
We can use any expression after $ to refer to a field.

NF indicates the number of fields and hence $NF will alwsys print last column.
NR indicates the  record number and hence $NR will print 1st field for 1st record, 2nd column for 2nd row  and so on.



bos90631:sm017r awk '{print $NF, $NR}' test1
100/900/200 sukul
100/800/300 8149122222
200/1000/500 Jhansi
200/9000/100 200/9000/100
100/800/300



We can use any expression after $. Notice the use of () to enclose the
expression.

Below example will print the 4th field.

bos90631:sm017r awk '{print $(2*2)}' test1
100/900/200
100/800/300
200/1000/500
200/9000/100
100/800/300


If result of expression is 0 then entire record will print.
Negative number are not allowed.

In below example (3-3) makes  it $0 and hence entire record is printed.

bos90631:sm017r awk '{print $(3-3)}' test1
sukul 8149158828 mumbai 100/900/200
uma 8149122222 chennai 100/800/300
bhanu 8097123451 Jhansi 200/1000/500
shushant 7798977047 nepal 200/9000/100
himanshu 9090909090 bokharo 100/800/300




Example 16: Changing value of any field

We can change the value of any field.
This does not change the value of the input file(awk never does that).
However internal copy of the record is changed . This means any change
did to the field is reflected on $0.

Below example changes the phone number field to 9999999999

bos90631:sm017r awk '/81491/ { $2="9999999999";print $0}' test1
sukul 9999999999 mumbai 100/900/200
uma 9999999999 chennai 100/800/300



Example 17: Creating New fields

We can create new fields also.
This creates the new field with appropriate field separators.

Adding new field changes the value of the internal copy of of $0.
To check whether appropriate number of field separators are added
to the field we use OFS built in variable which controls the
output field separator.

Creating a new field also changes the value to NF.
NF gets assigned to the highest field we create.
(Note that just refernceing the out of range field dos not change
the value of $0 or NF)

bos90631:sm017r awk 'BEGIN{ OFS=":"}{ $8="amdocs"; print $0}' test1
sukul:8149158828:mumbai:100/900/200::::amdocs
uma:8149122222:chennai:100/800/300::::amdocs
bhanu:8097123451:Jhansi:200/1000/500::::amdocs
shushant:7798977047:nepal:200/9000/100::::amdocs
himanshu:9090909090:bokharo:100/800/300::::amdocs








No comments:

Post a Comment