LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2004, 02:57 PM   #1
SirRobbin
LQ Newbie
 
Registered: May 2003
Location: State College, PA
Distribution: Red Hat 8.0
Posts: 17

Rep: Reputation: 0
Editing files from shell scripts?


I need to make a shell script that will go through a file and find every instance of a word and replace it with a new word. I know how to do this with vi:

:%s/old_word/new_word/g

but I need to do this automatically from a shell script. Does anyone know how to access a file and do this without having to pass control to vi?

Thanks
 
Old 03-22-2004, 03:03 PM   #2
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

Rep: Reputation: 30
cat filename |sed -e 's/old_word/new_word/g' > tempfile
rm filename; mv tempfile filename

Not sure if there's a clean way around the last part, but this definitely works.
 
Old 03-22-2004, 03:12 PM   #3
stabu
Member
 
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Rep: Reputation: 32
In Perlian:

perl -p -i -e 's/old_word/new_word/g' filename
 
Old 03-22-2004, 03:16 PM   #4
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

Rep: Reputation: 30
stabu's suggestion is better than mine.
 
Old 03-23-2004, 08:27 AM   #5
SirRobbin
LQ Newbie
 
Registered: May 2003
Location: State College, PA
Distribution: Red Hat 8.0
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for the help, i'll let you know how it works.
 
Old 03-23-2004, 10:12 AM   #6
SirRobbin
LQ Newbie
 
Registered: May 2003
Location: State College, PA
Distribution: Red Hat 8.0
Posts: 17

Original Poster
Rep: Reputation: 0
The perl command works great, but I have one question.

What would I do if in the command
perl -p -i -e 's/old_word/new_word/g' filename
I wanted to replace old_word or new_word with a variable in a shell script ($VAR). If i just put in $VAR it looks for $VAR and not the variable's expression.

Thanks again
 
Old 03-23-2004, 10:27 AM   #7
SirRobbin
LQ Newbie
 
Registered: May 2003
Location: State College, PA
Distribution: Red Hat 8.0
Posts: 17

Original Poster
Rep: Reputation: 0
Figured it out..

perl -p -i -e "s/$VAR/NEW_TXT/g" filename

it works cause of double quotes instead of single quotes
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Editing Slackware Install scripts comanighttrain Slackware 7 02-23-2006 05:35 PM
Editing Start up scripts question Neruocomp Red Hat 1 09-09-2005 01:43 PM
shell scripts newbie_m Linux - Newbie 3 01-18-2005 08:12 PM
I'm terrible at writing shell scripts. A little help fetching files? ashibaka Linux - General 6 04-10-2004 12:41 AM
Editing Files Via Shell mattsmith999 Linux - General 2 02-15-2004 12:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration