Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I invert a file 3

Status
Not open for further replies.

630111

MIS
Oct 30, 2001
127
0
0
US
Say I have a file as follows

january
february
march
april
may
june
july
august
september
october
november
december

I want to invert the file so that it reads

december
november
october
september
august
july
june
may
april
march
february
january

I don't want to sort it alphabetically, I want to invert it. What would I use or how would I go about it?

Thanks!
630111
 
Something like this ?
awk '{a[NR]=$0}END{for(i=NR;i;--i)print a}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi:

The easiest way is to use tail's reverse option:

tail -r datafile

However, that's not totally portable. My Red Hat 7.1 Linux tail command doesn't support -r. This Perl one-liner works where ever Perl is installed:

perl -e 'print reverse <>' datafile

Regards,


Ed
 
Its on my Sun box. Ah, oops, was in a set of GNU utilities we installed. Oh well ;-)
 
sed '1!G;h;$!d' myFile.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
sort -r filename

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
> sort -r filename

I believe that would actually sort it in reverse alphabetical order.
 
On HPUX there's an option to sort that lets you treat input lines as month names.

sort -M filname

so - and I haven't tried it but I would imagine that

sort -r -M filename

would give you what you want on HPUX at least

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Or perhaps:
Code:
grep -n . test.txt | sort -nr | cut -d: -f 2
;-)
 
too clever :) I like mine

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top