AWK with Examples 7


Example 26: Built in variable Output Format (OFMT)

When we print numbers awk converts them to string before printing.
It makes use of the function sprintf.

sprintf makes use of variable OFMT which indicates how to print numbers.

Default value of OFMT is %.6g

awk 'BEGIN {OFMT="%d"};{ print 17.35}' test1
This should print 17

(Does not work on my installation.)




Example 27: printf - formatted output


printf is used for output formatting.
We make use of formatting string when working with printf.

Note that printf does not add a newline character at the end.
We need to add newline character explicitly.

The output separators OFS and ORS do not have an effect on printf.

A format specifier starts with % and ends with a format control character.

Following are the format control characters:
c--> ascii character.
d--> decimal integer.
i--> decimal intteger
e--> exponential notation.
f--> floating notation.
g--> prints numbr in either scientific notation or floating point notation,whichever uses characters.
o--> usigned  octal integer
s--> string
x--> unsignd hexadecimal integer
%% --> % used to print a %

Modifiers for printf statments:

modifiers spcified between % and the format control letters.
1) -(minus): minus sign before the width modifier specifies left justification

2) 'width': specifies the desired width of output field.
The value is the minimum width and not the maximum width.
If the item is more in width it can be as wide as necessary.

3) .prec : specifies the precision to be used.
NUmber of digits to be printed after the decimal point.

awk 'BEGIN { printf "%-20s | %-20s | %-20s | %30s \n","NAME","PHONENO","CITY","SCORE"}
{ printf "%-20s | %-20s | %-20s | %30s \n",$1,$2,$3,$4 }' test1

pht022e2:/home/nemo_dev/sm017r> awk 'BEGIN { printf "%-20s | %-20s | %-20s | %30s \n","NAME","PHONENO","CITY","SCORE"}
{ printf "%-20s | %-20s | %-20s | %30s \n",$1,$2,$3,$4 }' test1>
NAME                 | PHONENO              | CITY                 |                          SCORE
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 28: Sending print/printf output to files
Normally print and printf both send output to the standard output which is screen.

But we can make the output go to other places like another file or another command.

To print output to the file we use print > "filename".
Note that filename should be inside double quotes.

awk ' {print $1 >"namefile"}
   {print $2 > "phonefile"} ' test1
  
This shows that wwe can can create multiple files over single read of the input file.


pht022e2:/home/nemo_dev/sm017r> more namefile
sukul
uma
bhanu
shushant
himanshu

pht022e2:/home/nemo_dev/sm017r> more phonefile
8149158828
8149122222
8097123451
7798977047
9090909090


We can also use >> to append to existing file.

Example 29: Sending the print output to a command as input

This means  giving print output as input to another command.
The command should be inside double quotes.
awk '{ print $1 > "namefile" ;print $1 | "sort -r > namefilesrt"}'  test1  
bos90631:sm017r more namefile
sukul
uma
bhanu
shushant
himanshu

bos90631:sm017r more namefilesrt
uma
sukul
shushant
himanshu
bhanu


Though we did not close the output file and command it is recommended
to close the command and file using the close function.

The file or the pipe stays open till we actually close the file/command
or awk exits.
close(filename)
close(command)

The command used to open the pipe should be used exactly to close it.

Reasons to close the command/file:
1) To start reading the file again from top in same awk program the file should be closed and then reopened.


2) To prevent exceeding number of files open at any given time.

3) If we are pipe data to a command, it buffers the data and only after we close it the command will execute
(or at the end of awk)


awk ' { report="mailx sukulm@gmail.com";
print "Awk  script worked" | report
print " Please check logs" | report
close(report) }' test1


Only when we close the report the maail command runs.

No comments:

Post a Comment