Print Redirection in AWK

Yesterday I made a mistake in AWK script, which I wrote to analysis the output of a TDMA-TDD simulation. In AWK script, I was trying to copy few specific lines to some other files like this:

print $0 > file.txt

When I run it, I got the following error message “Redirection has null string value”. Initially I was thinking in shell point of view. But later, I understood that, this is a bug in my AWK script. In AWK, file.txt was treated as variable name. file.txt is new variable and it will point to null value. So AWK is unable to redirect the message to this null string. To treat anything as a string, we have to quote it. After changing that line, like below, script started working successfully.

print $0 > “file.txt”

The same technique will work for printf also. Instead of literal filename, you can also use the variables. For example, we will take the student records and save the students in a file corresponding department:

CS RAM CS01
ME RAJA ME01
EE KUMAR EE01
EC RUPA EC01
CE BHARATHI CE01

print $3 $2 > $1

Advertisements
Tagged with: ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: