AWK with Examples 6

Example 21: getline with file (using variable)

getline var <"file1"


This reads the data from the file to a variable and hence does not override the value of $0.
The drawback of reading into a variable is that entire record goes to this variable and  we dont enjoy the benefit of being able to access individual fields as $0 is not changed.

 pht022e2:/home/nemo_dev/sm017r> awk '{ flag=0;
 while ((getline var < "lookup") > 0)
 {
 if (var==$1)
 { flag=1 }
 };
 if (flag==0)
 { print $1 " is the black sheep"};
 close("lookup")
 }' test1> > > > > > > > >
bhanu is the black sheep




Example 22 : getline with other command with pipe.
command | getline

The string  command is executed an the output is piped to awk as input.
The command must be enclosed inside quotes.
This allows to read one row at a time from the command output.
Note that this will change the value of $0 and NF.


pht022e2:/home/nemo_dev/sm017r> awk '{ command="date"; rc=(command | getline);
if (rc>0) {print $0}
close(command)}' test1> >
Wed Aug  8 05:52:51 CDT 2012
Wed Aug  8 05:52:51 CDT 2012
Wed Aug  8 05:52:51 CDT 2012
Wed Aug  8 05:52:51 CDT 2012
Wed Aug  8 05:52:51 CDT 2012


Note that we saved the command to a variable "command" and then used it with getline.
It is easier in case same command is executed several times ad also to close the command.
If not then we have to write the  exact command every time and can gget trouble some incase of long command.

Note that command has to be exact everytime it is referenced

We also have a variation as : command | getline var
This will not change the value of $0. Instead value read from the comand will be stored in the variable.




Example 23: print command in brief

print is used to print data to the standard output.
We can use print "" to print blank lines.
And we can also insert a \n to print data on two diferent lines using a single print command.
Note that the elements should be separated by comma for results to be separated by spaces.

pht022e2:/home/nemo_dev/sm017r> awk '{ print $1 "\n" $2 } ' test1
sukul
8149158828
uma
8149122222
bhanu
8097123451
shushant
7798977047
himanshu
9090909090






Example 24: Built in variable OUTPUT FIELD SEPARATOR(OFS)

OFS: stands for output field separtor.
Default value is spaces and hence we see values separated by spaces when we print data items separated by comma.
Basically this decides what characters to use to replace the comma we put
in the print command.


We can change it to any other value and the field separator in output will change.
Following wil change a space delimited file to a comma delimited file.
We generally set the value of OFS in BEGIN pattern so that it applies to all rows.

pht022e2:/home/nemo_dev/sm017r> awk 'BEGIN { OFS="," };{ print $0}' 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


pht022e2:/home/nemo_dev/sm017r> awk 'BEGIN { OFS="," };{ print $1,$2,$3,$4}' 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 25: Built in Variable OUTPUT RECORD SEPARATOR

ORS: outtput record separator.

Each print statement prints the ORS after  each string is printed.
Default value is \n and hence we mve in to the next record  after every print stetement.

We can set this value in BEGIN pattern so as to  apply to all records

pht022e2:/home/nemo_dev/sm017r> awk 'BEGIN{ ORS="OOOO"} ;{print $0}' test1
sukul 8149158828 mumbai 100/900/200OOOOuma 8149122222 chennai 100/800/300OOOObhanu 8097123451 Jhansi 200/1000/500OOOOshushant 7798977047 nepal 200/9000/100OOOOhimanshu 9090909090 bokharo 100/800/300OOOO



pht022e2:/home/nemo_dev/sm017r> awk 'BEGIN{ ORS="\n\n"} ;{print $0}' 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

Note that If the ORS does not contain a new line characters then all output will run on the same line.

No comments:

Post a Comment