The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace some line johnl Shell Programming and Scripting 1 05-22-2008 12:54 AM
Replace certain character with a new a new line. djsal Shell Programming and Scripting 8 01-11-2008 08:59 AM
search and replace the whole line Jartan Shell Programming and Scripting 17 09-25-2007 10:58 AM
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 1 09-25-2007 07:15 AM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 03:24 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
How to replace one line with three in sed

Hi,
I have a problem. I want to use SED to replace one line with three
It looks like:
PARAM='first_line,second_line,third_line'
And in file:
blablabla
blablabla
PARAM
blablabla
blablabla
blablabla

All I want is to change this into:

blablabla
blablabla
first_line
second_line
third_line

blablabla
blablabla
blablabla
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-05-2008
matt.d's Avatar
Registered User
 

Join Date: Feb 2008
Location: /dev/null
Posts: 62
I did it like:

Code:
sed 's/PARAM/first_line\nsecond_line\nthird_line/' file_with_blablabla
Reply With Quote
  #3 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,434
Code:
awk '/PARAM/{print "firstline\nsecondline\nthirdline";next}1' file
Reply With Quote
  #4 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 482
Export the PARAM variable to the environment otherwise whatever solution you use will not work.

Code:
export PARAM='first_line,second_line,third_line'

awk '/PARAM/{system("echo $"$0)};!/PARAM/' file | tr ',' '\n'
Reply With Quote
  #5 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
Quote:
Originally Posted by matt.d View Post
I did it like:

Code:
sed 's/PARAM/first_line\nsecond_line\nthird_line/' file_with_blablabla
It returns
.......
blablabla
first_linensecond_linenthird_line
blablabla
..........

Thx for awk scripts but this is a part of bigger program and I have to use SED
Reply With Quote
  #6 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 482
Quote:
Originally Posted by ch0sen View Post
Thx for awk scripts but this is a part of bigger program and I have to use SED
Code:
sed 's/PARAM/'$PARAM'/' file | tr ',' '\n'
Reply With Quote
  #7 (permalink)  
Old 03-05-2008
Registered User
 

Join Date: Mar 2008
Posts: 3
That should work just fine.
Thank You
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0