LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Closed Thread
 
Search this Thread
Old 11-01-2004, 04:07 PM   #1
matthurne
Member
 
Registered: May 2002
Distribution: Fedora Core
Posts: 41

Rep: Reputation: 15
Search and replace text in file using shell script?


I am writing a shell script to perform a setup for all the teams of a class project. I'm at the point where the same files are copied into each team's home directory. The next step is to edit the same configuration file for each team and change a couple of values in that file. For example, a port number in the resin.conf file needs to be changed to 808x for each team where x is the team number. How can I search and replace text easily in my script? Here's my script so far (which is working just the way I want it to):

Code:
#!/bin/bash
#install.sh
#Installs resin into all the user directories

#Modify these paths as you feel appropriate
Pth="/home/csc332t"
USERNME="csc332t"

resinVers="3.0.9"
resinPkg="resin-$resinVers.tar.gz"
resinDir="resin-$resinVers"

tar zxf $resinPkg
cd $resinDir
./configure --prefix=/home/csc332ref/resin
make
make install

i=1

while [ $i -le 8 ]
do
        mkdir /home/csc332t$i
        cp -R /home/csc332ref/* /home/csc332t$i
        i=$(($i+1))
done
 
Old 11-01-2004, 07:11 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
The 'sed' utility is what you want. It can search and replace text using regular expressions. You should be able to 'cat' the resin.conf file into sed, do the replacement, and redirect the output back to resin.conf, something like this:

Code:
cat resin.conf | sed -e 's/ABC/XYZ/' > resin.conf
where ABC is a regular expression matching what you want to find, and XYZ is what you want to replace it with. There's a sed micro-primer in the advanced bash guide that may help you figure out how to insert a variable name into a sed expression.

Last edited by wapcaplet; 11-01-2004 at 07:14 PM.
 
Old 11-02-2004, 10:11 AM   #3
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,694

Rep: Reputation: 45
other thread is here:
http://www.linuxquestions.org/questi...hreadid=249960
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 05:59 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM


All times are GMT -5. The time now is 03:24 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration