Home > awk, Interesting > Interesting – Print certain lines in reverse (awk)

Interesting – Print certain lines in reverse (awk)

Problem:
You would like to print certain lines in a file in reverse.

Solution:
awk ‘{if (NR % 2) {print} else {r=”rev”; print | r; close(r);}; }’ myfile

$less myfile
kitten
pig
horse
cow
donkey
$awk '{if (NR % 2) {print} else {r="rev"; print | r; close(r);}; }' myfile
kitten
gip
horse
woc
donkey

Above awk prints all even lines in from file in reverse.

Note:
NR % 2 – Returns 0 for even lines which is false. So odd lines which are 1 == true, are simply printed.
rev – bash command that reverses a line. (man rev)
close(r) – Need to explicitly close the pipped command so that the output can be flushed.
r=”rev” ; print | r; close(r);}; – Print the line, pass it to rev to reverse it, flush output so that results can be printed immediately.

More on close()

close(filename)
or
close(command)

The argument filename or command can be any expression. Its value must exactly match the string that was used to open the file or start the command (spaces and other “irrelevant” characters included). For example, if you open a pipe with this:

“sort -r names” | getline foo

then you must close it with this:

close(“sort -r names”)

Here are some reasons why you might need to close an output file:

To write a file and read it back later on in the same awk program. Close the file when you are finished writing it; then you can start reading it with getline.

To write numerous files, successively, in the same awk program. If you don’t close the files, eventually you may exceed a system limit on the number of open files in one process. So close each one when you are finished writing it.

To make a command finish. When you redirect output through a pipe, the command reading the pipe normally continues to try to read input as long as the pipe is open. Often this means the command cannot really do its work until the pipe is closed. For example, if you redirect output to the mail program, the message is not actually sent until the pipe is closed.

To run the same program a second time, with the same arguments. This is not the same thing as giving more input to the first run! For example, suppose you pipe output to the mail program. If you output several lines redirected to this pipe without closing it, they make a single message of several lines. By contrast, if you close the pipe after each line of output, then each line makes a separate message.

Source:
http://www.unix.com/showthread.php?p=302869631
http://www.chemie.fu-berlin.de/chemnet/use/info/gawk/gawk_7.html#SEC67

Advertisements
Categories: awk, Interesting
  1. No comments yet.
  1. No trackbacks yet.

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: