Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


Closed Thread
  Search this Thread
Old 11-01-2004, 05:07 PM   #1
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):

#Installs resin into all the user directories

#Modify these paths as you feel appropriate


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


while [ $i -le 8 ]
        mkdir /home/csc332t$i
        cp -R /home/csc332ref/* /home/csc332t$i
Old 11-01-2004, 08:11 PM   #2
LQ 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:

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 08:14 PM.
Old 11-02-2004, 11:11 AM   #3
Senior Member
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
other thread is here:

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 01:13 AM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 06:59 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 04:07 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 06:37 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:10 PM.

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