6.14. Redirection and Pipes
6.14.1 Output Redirection
When redirecting output from within awk to a UNIX/Linux file, the shell redirection operators are used. The filename must be enclosed in double quotes. When the > symbol is used, the file is opened and truncated. Once the file is opened, it remains open until explicitly closed or the awk program terminates. Output from subsequent print statements to that file will be appended to the file.
The >> symbol is used to open the file, but does not clear it out; instead it simply appends to it.
Example 6.96.
% nawk '$4 >= 70 {print $1, $2 > "passing_file" }' filename
EXPLANATION
If the value of the fourth field is greater than or equal to 70, the first and second fields will be printed to the file passing_file.
6.14.2 Input Redirection (getline)
The getline Function
The getline function is used to read input from the standard input, a pipe, or a file other than the current file being processed. It gets the next line of input and sets the NF , NR, and the FNR built-in variables. The getline function returns 1 if a record is found and 0 if EOF (end of file) is reached. If there is an error, such as failure to open a file, the getline function returns a value of –1.
Example 6.97.
% nawk 'BEGIN{ "date" | getline d; print d}' filename
Thu Jan 14 11:24:24 PST 2004
EXPLANATION
Will execute the UNIX/Linux date command, pipe the output to getline, assign it to the user-defined variable d, and then print d.
Example 6.98.
% nawk 'BEGIN{ "date " | getline d; split( d, mon) ; print mon[2]}' filename
Jan
EXPLANATION
Will execute the date command and pipe the output to getline. The getline function will read from the pipe and store the input in a user-defined variable, d. The split function will create an array called mon out of variable d and then the second element of the array mon will be printed.
Example 6.99.
% nawk 'BEGIN{while("ls" | getline) print}'
a.out
db
dbook
getdir
file
sortedf
EXPLANATION
Will send the output of the ls command to getline; for each iteration of the loop, getline will read one more line of the output from ls and then print it to the screen. An input file is not necessary, because the BEGIN block is processed before awk attempts to open input.
Example 6.100.
(The Command Line)
1 % nawk 'BEGIN{ printf "What is your name?" ;\
getline name < "/dev/tty"}\
2 $1 ~ name {print "Found " name " on line ", NR "."}\
3 END{print "See ya, " name "."}' filename
(The Output)
What is your name? Ellie < Waits for input from user >
Found Ellie on line 5.
See ya, Ellie.
EXPLANATION
Will print to the screen What is your name? and wait for user response; the getline function will accept input from the terminal (/dev/tty) until a newline is entered, and then store the input in the user-defined variable name. If the first field matches the value assigned to name, the print function is executed. The END statement prints out See ya, and then the value Ellie, stored in variable name, is displayed, followed by a period.
Example 6.101.
(The Command Line)
% nawk 'BEGIN{while (getline < "/etc/passwd" > 0 )lc++; print lc}' file
(The Output)
16
EXPLANATION
Awk will read each line from the /etc/passwd file, increment lc until EOF is reached, then print the value of lc, which is the number of lines in the passwd file.
Note:
The value returned by getline is –1 if the file does not exist. If the EOF is reached, the return value is 0, and if a line was read, the return value is 1. Therefore, the command
while ( getline < "/etc/junk")
would start an infinite loop if the file /etc/junk did not exist, because the return value of –1 yields a true condition.
|