Reverse Display of a file

Hi all,

Just saw a "sed" format to reverse display the file contents, but am not geting its logic completely. I would appreciate if somebody can explain

sed '1!G;h;$!d'  <  filename

All I know in this is that :

G will add a new line after every line except first one ( 1!G )
In the end part , d will display the last line ( $!d )

I have tested them separately but how the combination works to reverse the file contents.

Thanks,

Dexter

This has the answer. Please check answer 2.

Unix Sed Tutorial : 7 Examples for Sed Hold and Pattern Buffer Operations (link removed)

Doesn't your OS have the tac command or the -r option for the tail command ?

tail -r yourfile
tac yourfile

Take a look at the

tac

command.

Get it... cat spelled backwards.

:smiley:

Thanks Rajamadhavan for the link of explaination.

and others - Yes , I don't have that -r option and tac command on my OS.

Dexter

As far as I understand, you do not need tac nor tail -r , only like to understand how sed '1!G;h;$!d' < filename works?

Just for the fun of it:

awk '{out=$0 s out;s=RS} END {print out}' file
awk '{a[NR]=$0} END {for(i=NR;i>0;i--) print a}' file
cat -n file | sort -k1,1rn | cut -f 2-
perl -e 'print reverse <>' file
+++