The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
dynamic editing using shell script habzone2007 Shell Programming and Scripting 1 05-14-2008 07:55 PM
editing a file via a shell script ?? jimmyc Shell Programming and Scripting 2 03-28-2008 08:21 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 01:25 AM
Editing files in shell programing nir_s Shell Programming and Scripting 0 07-13-2005 01:53 AM
Editing a file using a script Ypnos Shell Programming and Scripting 45 07-02-2003 06:39 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-07-2005
Registered User
 

Join Date: Feb 2005
Posts: 2
Editing a file in a shell script

Using Solaris 8.
I need to create a shell script that will edit a text file.
I need to look in the text file and do a search and replace. For instance, the text file name is always 'filename'. I need to open filename and replace every instance of 'oldtext' with 'newtext'. 'oldtext' is static. newtext is a variable that I will pass to the shell via the 'read' command further up in the script.
I've got most of the script written....I can pass the variables and create directorys, copy files etc, but I don't know how to edit a text file in a shell.

Thanks,
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-07-2005
Registered User
 

Join Date: Feb 2005
Posts: 2
Nevermind

I figured it out. I was thinking I had to open it in vi and then do the search and replace in there - but I figured out what sed was and just put that in the script.
sed "/s/oldtext/newtext/g" filename > newfilename
Reply With Quote
  #3 (permalink)  
Old 02-07-2005
Registered User
 

Join Date: Jan 2005
Posts: 97
Quote:
Originally Posted by jowpup
I figured it out. I was thinking I had to open it in vi and then do the search and replace in there - but I figured out what sed was and just put that in the script.
sed "/s/oldtext/newtext/g" filename > newfilename
Try this which is appropriate if you want to read in values from a shell variable all the time

cat oldfile | sed -e "s/testpattern/$REPL/g" > newfile
Reply With Quote
  #4 (permalink)  
Old 02-08-2005
encrypted's Avatar
Registered User
 

Join Date: Feb 2004
Location: Oslo, Norway
Posts: 218
Or else, if you have perl
perl -p -i -e 's/oldtext/newtext/' <filename>
would change the file <filename> without creating a new file
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:08 AM.


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