LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2012, 10:23 PM   #1
Karljoe2
LQ Newbie
 
Registered: Apr 2012
Distribution: RHEL ,CentOS,Fedora
Posts: 7

Rep: Reputation: Disabled
Reverse the order of lines using sed


Greetings!

I have a text file as below:
Linux
AIX
Windows
Mac

In order to reverse the order of lines("Linux" last and "Mac" first) I found this sed command.Can anyone explain its working.

sed -n '1!G;h;$p' <text file>

Thanks in advance.
 
Old 12-24-2012, 11:33 PM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Run in terminal `info sed tac' and read the explanation.

Last edited by firstfire; 12-24-2012 at 11:35 PM.
 
1 members found this post helpful.
Old 12-25-2012, 06:30 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
By amazing coincidence, I was playing around with this exact command earlier today, as an exercise in understanding the hold buffer.

(Actually, not that much of a coincidence. It was your other thread that prompted me to do so.)

As I mentioned there, see grymoire tutorial, particularly the section on multi-line commands, for the full rundown on the commands.

There's also a good blog that has breakdowns of of one-liner commands like this in sed and awk and such.

http://www.catonmat.net/blog/sed-one...ined-part-one/

(This one is #36 on the above page)


But to try to explain it myself, lets break the expression up into it's individual commands:

Code:
sed -n
Don't print lines by default.

Code:
'1!G
Ignore the first line, but for every subsequent line append the contents of the hold buffer to the current line in the pattern buffer, separated by a newline character.

Code:
h
Copy the modified contents of the pattern buffer to the hold buffer, replacing the previous contents. Remember that the first line was unmodified by the previous command, so it's copied as-is.

Code:
$p'
On the final line, print the contents of the pattern buffer, after having the hold contents appended to it in the first command. Note that since this is the last line, the 'h' command in the final step runs, but has no further effect.


So what it does is store a copy of line one, then when it reaches line two, attaches that copy to the end of it, stores the result, appends the both of them to line 3, and so on to the end of the file, when it prints the whole thing.

But yeah, tac is probably a better choice for this particular job. Although the sed command is more flexible since it could be modified to do other things like extracting and reversing only certain lines.

Last edited by David the H.; 12-25-2012 at 06:55 AM. Reason: clarifications
 
1 members found this post helpful.
Old 12-25-2012, 11:32 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Quote:
Originally Posted by David the H. View Post
But yeah, tac is probably a better choice for this particular job. Although the sed command is more flexible since it could be modified to do other things like extracting and reversing only certain lines.
I believe you missed my point. The command I provided, which mentions tac, gives the following output
Code:
File: sed.info,  Node: tac,  Next: cat -n,  Prev: Reverse chars of lines,  Up: Examples

4.6 Reverse Lines of Files
==========================

This one begins a series of totally useless (yet interesting) scripts
emulating various Unix commands.  This, in particular, is a `tac'
workalike.

   Note that on implementations other than GNU `sed' this script might
easily overflow internal buffers.

     #!/usr/bin/sed -nf

     # reverse all lines of input, i.e. first line became last, ...

     # from the second line, the buffer (which contains all previous lines)
     # is *appended* to current line, so, the order will be reversed
     1! G

     # on the last line we're done -- print everything
     $ p

     # store everything on the buffer again
     h
It is the info page from sed documentation. I just hoped to help the OP to discover this excellent source of information.
 
2 members found this post helpful.
Old 12-26-2012, 10:38 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Oops, yeah. I did miss that. Mea culpa. Didn't read your follow-up carefully enough.

At least now the OP has plenty of resources to look into!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can I set the cups output order to always print in reverse order Thane Ubuntu 3 07-09-2018 09:49 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
reverse order some lines of text files Melsync Programming 4 09-20-2005 04:40 PM
Reverse Order Printing lawrencegoodman Linux - Newbie 2 11-13-2003 07:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration