AWK - Output Redirection

So far, we displayed data on standard output stream. We can also redirect data to a file. A redirection appears after the printor printf statement. Redirections in AWK are written just like redirection in shell commands, except that they are written inside the AWK program. This chapter explains redirection with suitable examples.

Redirection Operator

The syntax of the redirection operator is −

Syntax

print DATA > output-file
It writes the data into the output-file. If the output-file does not exist, then it creates one. When this type of redirection is used, the output-file is erased before the first output is written to it. Subsequent write operations to the same output-file do not erase the output-file, but append to it. For instance, the following example writes Hello, World !!! to the file.
Let us create a file with some text data.

Example

[jerry]$ echo "Old data" > /tmp/message.txt
[jerry]$ cat /tmp/message.txt
On executing this code, you get the following result −

Output

Old data
Now let us redirect some contents into it using AWK's redirection operator.

Example

[jerry]$ awk 'BEGIN { print "Hello, World !!!" > "/tmp/message.txt" }'
[jerry]$ cat /tmp/message.txt
On executing this code, you get the following result −

Output

Hello, World !!!

Append Operator

The syntax of append operator is as follows −

Syntax

print DATA >> output-file
It appends the data into the output-file. If the output-file does not exist, then it creates one. When this type of redirection is used, new contents are appended at the end of file. For instance, the following example appends Hello, World !!! to the file.
Let us create a file with some text data.

Example

[jerry]$ echo "Old data" > /tmp/message.txt 
[jerry]$ cat /tmp/message.txt
On executing this code, you get the following result −

Output

Old data
Now let us append some contents to it using AWK's append operator.

Example

[jerry]$ awk 'BEGIN { print "Hello, World !!!" >> "/tmp/message.txt" }'
[jerry]$ cat /tmp/message.txt
On executing this code, you get the following result −

Output

Old data
Hello, World !!!

Pipe

It is possible to send output to another program through a pipe instead of using a file. This redirection opens a pipe to command, and writes the values of items through this pipe to another process to execute the command. The redirection argument command is actually an AWK expression. Here is the syntax of pipe −

Syntax

print items | command
Let us use tr command to convert lowercase letters to uppercase.

Example

[jerry]$ awk 'BEGIN { print "hello, world !!!" | "tr [a-z] [A-Z]" }'
On executing this code, you get the following result −

Output

HELLO, WORLD !!!

Two way communication

AWK can communicate to an external process using |&, which is two-way communication. For instance, the following example uses tr command to convert lowercase letters to uppercase. Our command.awk file contains −

Example

BEGIN {
   cmd = "tr [a-z] [A-Z]"
   print "hello, world !!!" |& cmd
   close(cmd, "to")
   
   cmd |& getline out
   print out;
   close(cmd);
}
On executing this code, you get the following result −

Output

HELLO, WORLD !!!
Does the script look cryptic? Let us demystify it.
  • The first statement, cmd = "tr [a-z] [A-Z]", is the command to which we establish the two-way communication from AWK.
  • The next statement, i.e., the print command provides input to the tr command. Here &| indicates two-way communication.
  • The third statement, i.e., close(cmd, "to"), closes the to process after competing its execution.
  • The next statement cmd |& getline out stores the output into out variable with the aid of getline function.
  • The next print statement prints the output and finally the close function closes the command.

Contact

Talk to us

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores iusto fugit esse soluta quae debitis quibusdam harum voluptatem, maxime, aliquam sequi. Tempora ipsum magni unde velit corporis fuga, necessitatibus blanditiis.

Address:

৪৭/ দক্ষিণ বেগুনবাড়ি তেজগাঁও শিল্প এলাকা ঢাকা ১২০৮

Work Time:

সোমবার - শুক্রবার থেকে সকাল 9 টা থেকে বিকাল 5 টা

Phone:

+88 019 4145 2627