Register
It is currently Tue Oct 21, 2014 4:21 am

REQUEST: a script that flips a text file (first<->last


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu Aug 14, 2008 6:21 am   

Joined: Thu Aug 14, 2008 6:18 am
Posts: 4
Hi,

I'm looking for a script that does the following to a text file:

for example, my textfile contains:

root> cat test.txt
line1
line2
line3

I need a script that converts it to:

root> cat test-new.txt
line3
line2
line1

I know there must be an easyway but I can't figure out.

You help is appreciated

Thanks in advance


Top
 Profile  
 PostPosted: Thu Aug 14, 2008 3:25 pm   
User avatar

Joined: Sun Apr 13, 2008 4:05 am
Posts: 37
Location: /dev/random
Code:
cat text.txt | sort -r


thats all


Top
 Profile  
 PostPosted: Thu Aug 14, 2008 4:06 pm   

Joined: Thu Aug 14, 2008 6:18 am
Posts: 4
That's kinda simple.
I've been thinking too complicated about this.

thanks


Top
 Profile  
 PostPosted: Fri Aug 15, 2008 10:39 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
The problem with that is that sort will do exactly what it says -- sort the lines.

Code:
< inputfile > outputfile perl -e '
my @input;
while (<>) { push @input, $_ }
print foreach (reverse @input);'


Top
 Profile  
 PostPosted: Wed Aug 27, 2008 9:08 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
There is a simple and elegant solution, and I can't believe nobody's beaten me to posting it!

The mighty "tac"!

Code:
$ for i in `seq 1 3`; do echo "line $i" >> test.txt; done

$ cat test.txt
line 1
line 2
line 3

$ tac test.txt
line 3
line 2
line 1


Hope this helps!
-Jeo


Top
 Profile YIM  
 PostPosted: Wed Aug 27, 2008 9:14 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
There's also this little snippet from the "Handy one liners for awk" if you like that sort of thing :)

Code:
# reverse order of lines (emulates "tac")
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*


Top
 Profile YIM  
 PostPosted: Tue Sep 02, 2008 11:05 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Ohh, I forgot about tac. Nice one!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP