Register
It is currently Sun Apr 20, 2014 2:57 pm

Help with sed command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Tue Apr 13, 2010 4:26 am   

Joined: Tue Apr 13, 2010 4:24 am
Posts: 1
Hi everyone, i wonder if someone can help me with this script:

sed -e :a -e '$q;N;11,$D;ba'

this script is supposed to emulate the tail command, can you please explain me the meaning of all the parameters such as -e, :a, $q, N, 11, $D, ba?

Thanks for your help. :))


Top
 Profile  
 PostPosted: Mon May 03, 2010 1:52 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Well, that's the most goddamn dense sed command I've ever seen.

-e : Adds a script to be executed.

:a : Creates a label 'a' for the script that follows...

$q : The '$' matches the last line. The q quits sed. Basically, quit if we are at the end.

N : Appends the line.

11,$D : This one is tricky... "11,$" matches the 11th through the last line. D basically deletes the last line, leaving us with 10.

ba : Branches back to label 'a'. Basically, start over again.

Now, I still don't know for sure how all of that works together, but it does.

- thobbs


Top
 Profile  
 PostPosted: Mon May 03, 2010 4:07 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
thobbs wrote:
...
N : Appends the line.

11,$D : This one is tricky... "11,$" matches the 11th through the last line. D basically deletes the last line, leaving us with 10.

ba : Branches back to label 'a'. Basically, start over again.

Now, I still don't know for sure how all of that works together, but it does.

- thobbs

The command appends lines. Starting from line 11, it also delets the oldest line when adding a new one.


Top
 Profile  
 PostPosted: Tue May 11, 2010 5:56 pm   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
This command will begin reading the first line, in this case the command:
- '$q" wiil do nothing (if it is not the last line)
- 'N' will append the first line to the second
- '11,$D' will do nothing, since it is not yet the line 11
- ':ba' will go back to 'a'

The process will continue until sed gets the line 11 (if the text has less than 11 lines, then it will just print all lines)

After getting the line 11 something changes, let's see:

- '$q' will quit just if it is the last line.
- 'N" append the line number 11 to the others
-'11,$D' will delete just the first line (as it is said in the man of the sed: "Delete up to the first embedded newline in the pattern space")
-':ba' will restart the process

From now on the first line of the stream will be always deleted in each cycle, so just the last 11 lines will remain until the last line be reached.

I think that is all.
That was hard to understand.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 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:  


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