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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-29-2006
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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
  #2 (permalink)  
Old 09-29-2006
gauravgoel gauravgoel is offline
Registered User
  
 

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

Last edited by gauravgoel; 09-29-2006 at 08:58 AM.. Reason: wrong solution
  #3 (permalink)  
Old 09-29-2006
gauravgoel gauravgoel is offline
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 09:10 AM..
  #4 (permalink)  
Old 09-29-2006
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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 ??
  #5 (permalink)  
Old 09-29-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398

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

  #6 (permalink)  
Old 09-29-2006
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0