Nityanand's Weblog

  • view my stats

    free page hit counter
  • Earn Money by viewing ads

  • Useful Links

Posts Tagged ‘reverse order’

How to print a file in reversed order in UNIX

Posted by Nityanand Dubey on August 23, 2010

 

Requirement : Print a file in such a manner that the last line should come at the first like a LIFO

Lets create one example of a file (MyFile) containing following three lines

A
B
C

To print a file in a shell we use the cat command

%cat MyFile
this will print all the contents in a file sequentially.

Output :
A
B
C

%cat –n MyFile
This will print the content with the line numbers

Output :
1 A
2 B
3 C

%cat -n MyFile | sort –nr
This will print the lines into reverse order

Output :

3 C
2 B
1 A

%cat -n MyFile | sort –nr | cut –c 8-

Output :
C
B
A

This will cut the initial 8 characters from each line and prints the desired output.

Posted in SHELL | Tagged: , , , | 1 Comment »