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
overwrite specific lines in a file csecnarf Shell Programming and Scripting 7 05-13-2008 08:01 AM
how to remove specific lines from a file bluemoon1 Shell Programming and Scripting 17 10-07-2007 07:40 PM
replacing first line or lines in a file Terrible UNIX for Advanced & Expert Users 3 06-28-2006 05:23 PM
Update specific lines in a file aukequist Shell Programming and Scripting 3 11-15-2005 10:52 AM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-22-2005 09:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-29-2006
Registered User
 

Join Date: Mar 2002
Posts: 194
replacing specific lines in a file

Hi there
I have a file which has the lines


Code:
# Serial number for hostid
EXP_SERIAL_=""
These lines could be anywhere in the file as far as line numbers go, I would like replace these two lines with

Code:
# Serial number for hostid $var1
EXP_SERIAL_$var1="$var2"
Is there a quick and simple way for me to do this ?

cheers
Reply With Quote
Forum Sponsor
  #2  
Old 09-29-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
........................................

Last edited by gauravgoel; 09-29-2006 at 04:58 AM. Reason: wrong solution
Reply With Quote
  #3  
Old 09-29-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
use this
Quote:
sed "s/old text/new text/g" input.txt
though not clean, should work

Last edited by gauravgoel; 09-29-2006 at 05:10 AM.
Reply With Quote
  #4  
Old 09-29-2006
Registered User
 

Join Date: Mar 2002
Posts: 194
thats great but if i run that command from the command line it outputs the correct result to the screen but doesnt actually edit the file


How do i get it to make a permanent change to the file ??
Reply With Quote
  #5  
Old 09-29-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
sed "s/old text/new text/g" input.txt > temp
mv temp input.txt
or

Code:
perl -pi -e "s/old text/new text/g" input.txt
Reply With Quote
  #6  
Old 09-29-2006
Registered User
 

Join Date: Mar 2002
Posts: 194
that works great thankyou ....I wanted to avoid creating a new file and mv'ing it back over again....but if i have to do that then so be it

thanks for your help
Reply With Quote
  #7  
Old 09-30-2006
mph mph is offline
Registered User
 

Join Date: Mar 2006
Posts: 44
You might try something like this if you don't want to create a file:
Code:
echo '%s/oldtext/newtext/g\nwq' | ex file
This will edit the text within the file. Non interactive style vi. The exact syntax for ex is a bit murky I can't find a lot of docs on it, but in a few occasions it's well worth figuring out and using.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:49 AM.


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

Content Relevant URLs by vBSEO 3.2.0