Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-20-2008, 11:58 PM   #1
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
Putting blank line after the search pattern.

i can make you use of these code to place a new line after the pattern my_word in the file.

sed '/my_word/{x;p;x;}'

But the thing is that how the exchange of the buffers happens ?
and how execution of each line happen or whole fie processed in the buffer. ?
Old 09-21-2008, 04:29 AM   #2
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Actually, the code you show would print a newline *before* a line containing my_word:

$ echo -e 'hello\nmy_word\ngoodbye' | sed '/my_word/{x;p;x;}'   

Sed works by reading and copying the current input line into what it calls "the pattern space" (deleting the newline). Then commands are performed on that pattern space (think of it as a machine register that points to a string). Sed also has a second register called "the hold space" (also a machine register that points to another string).

The x (exchange0 command swaps the hold space with the pattern space..

So your command takes a line, swaps the that line (minus the newline) with the hold space (which is empty). So the pattern space is now empty, and the hold space contains the input line (minus the newline). It them prints (the p command) the contents of the pattern space (empty, so a newline is printed). Then, once again, you swap pattern and hold. Finally, sed's default action is to print the pattern space, so the matched line is printed.
Old 09-21-2008, 08:38 AM   #3
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Your code will not "place a new line after the pattern my_word in the file", but rather "place a new line after the line containing the pattern my_word in the file". I hope that is what you want, even though it isn't what you say.

Why are you messing w/ the "x" & "p" commands, when "a" (append) will do the job much more simply?:
sed '/my_word/a\\'

If, OTOH, you really do want to insert a new line directly after the pattern "my_word" in the file (breaking the line it's in at "my_word"), then this should work:
sed 's,\<my_word\>,&\n,g'
In case you're wondering, from the grep, not sed, man page:
The symbols \< and \> respectively match the empty string at the beginning and end of a word.



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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 07:17 AM
vim search pattern Berticus General 3 08-21-2008 01:13 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 09:13 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 05:50 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 PM.

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