7.2. Redirection and Pipes

7.2.1. Output Redirection

When redirecting output from within awk to a UNIX 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 opened 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 7.10.
% awk '$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.

7.2.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 from 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 one if a record is found and zero 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 7.11.
% awk 'BEGIN{ "date" | getline d; print d}' filename
							Thu Jan 14 11:24:24 PST 2000
						

Explanation

Will execute the UNIX date command, pipe the output to getline, assign it to the user-defined variable d, and then print d.

Example 7.12.
% awk 'BEGIN{ "date " | getline d; split( d, mon) ; print mon[2]}'
							filename
							Oct
						

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 7.13.
% awk ' 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 7.14.
(The Command Line)
1  % awk '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

  1. 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.

  2. If the first field matches the value assigned to name, the print function is executed.

  3. The END statement prints out "See ya," and then the value Ellie, stored in variable name, is displayed.

Example 7.15.
(The Command Line)

% awk '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, and then print the value of lc, which is the number of lines in the passwd file.

Note

The value returned by getline is minus one if the file does not exist. If the end of file is reached, the return value is zero, and if a line was read, the return value is one. 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 minus one yields a true condition.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.234.150