sed.. Add new line after finding text | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sed.. Add new line after finding text

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-30-2004
douknownam douknownam is offline
Registered User
 
Join Date: May 2004
Last Activity: 22 July 2011, 3:53 PM EDT
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
sed.. Add new line after finding text

I know that this my be really simple, but I'm having a hard time accomplishing it. I am trying to add a new line of text after finding a particular string of text in a file. Here's what I'm getting:

Code:
sed: command garbled: N/search_string/\\new_text/

I was using "N" to add a line after the text. TIA.
Sponsored Links
    #2  
Old 11-30-2004
bhargav's Avatar
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 26 July 2012, 3:38 PM EDT
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts
try this ...


sed '/patternstring/ i\
new line string' file1


Please see a new line in the command between i\ and new string.
Sponsored Links
    #3  
Old 11-30-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 13 May 2013, 9:02 AM EDT
Location: Melbourne, Australia
Posts: 2,185
Thanks: 3
Thanked 24 Times in 23 Posts
You could also use awk, which I find gives you a bit more control than sed in these situations

Code:
awk '{ if ( $0 ~ /searchstring/ ) {
          printf( "%s\n%s\n", "Some new inserted text here", $0 );
	  # could use the following to append
	  # printf( "%s\n%s\n", $0, "Some new appended text here" );
     } else {
          print $0;
     }
}' my_input_file

Cheers
ZB
    #4  
Old 12-01-2004
douknownam douknownam is offline
Registered User
 
Join Date: May 2004
Last Activity: 22 July 2011, 3:53 PM EDT
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you, that worked like a charm. However, I just used a\ instead of i\ to insert the line after the search string.

sed '/patternstring/ a\
new line string' file1
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reformatting single column text file starting new line when finding particular string kieranfoley Shell Programming and Scripting 7 10-21-2011 02:02 PM
help with finding text and deleting line ichar Shell Programming and Scripting 3 07-15-2010 09:23 AM
Finding a string in a text file and posting part of the line busdude Shell Programming and Scripting 13 03-25-2010 11:20 AM
adding new line after finding specific text mscice Shell Programming and Scripting 9 06-08-2009 08:55 AM
Finding pattern & prepending a line with text kirrushna Shell Programming and Scripting 1 07-11-2006 02:20 AM



All times are GMT -4. The time now is 07:56 PM.