LinuxQuestions.org
Help answer threads with 0 replies.
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 09-20-2008, 10:58 PM   #1
dina3e
Member
 
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, 03:29 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

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

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

my_word
goodbye
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, 07:38 AM   #3
archtoad6
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?:
Code:
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:
Code:
sed 's,\<my_word\>,&\n,g'
In case you're wondering, from the grep, not sed, man page:
Quote:
The symbols \< and \> respectively match the empty string at the beginning and end of a word.
 
  


Reply

Tags
sed


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 06:17 AM
vim search pattern Berticus General 3 08-21-2008 12: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 08:13 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM
Pattern search in a line jitz Linux - General 2 12-06-2003 04:50 AM

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

All times are GMT -5. The time now is 10:08 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