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
Molecular biologist requires help re: search / replace script gstuart Shell Programming and Scripting 11 04-09-2008 02:08 PM
search & replace password perl script shellscript22 Shell Programming and Scripting 4 03-25-2008 12:17 PM
Need to search and replace in multiple files in directory hierarchy umen Shell Programming and Scripting 3 12-24-2007 01:56 AM
Multiple search and replace tungaw2004 UNIX for Dummies Questions & Answers 1 03-21-2007 09:03 PM
search and replace dynamic data in a shell script csejl Shell Programming and Scripting 8 10-21-2003 07:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-21-2007
Registered User
 

Join Date: Feb 2007
Posts: 31
multiple input search and replace script

hi,
i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files.

cho ""
echo "Domain_name "
read domain_name
echo "Work path Name : "
read wk_path_name
echo "Server Name 1: "
read server1name
echo "Admin Port: 1: "
read adminport1
echo "Server Name 2: "
read servername2

echo "Admin Port: 2: "
read adminport2
echo "Multicast Addr :"
read mcast_addr
echo "Multicast Port :"
read mcast_port

for i in list.txt do
cat $i |sed -e 's/oldpattern/$domain_name/g' $i > $i".new"
done

Problems
1. how to pass the values entered from "read" command thru the "oldpattern"?
2. how to pass the values without creating a new file? is there any way.
Reply With Quote
Forum Sponsor
  #2  
Old 03-21-2007
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 435
For your qn1,
below one will be a solution...

read oldval
read newval
while read line
do
{
sed 's/'$oldval'/'$newval'/g' $line >$line".new"
}
done<list.txt

For your qn2.,
I believe there is no other way other than creating a temp file which contains the output of sed command and then renaming it to the original filename...
Reply With Quote
  #3  
Old 03-21-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
For q2, GNU sed has -i option
Reply With Quote
  #4  
Old 04-29-2007
Registered User
 

Join Date: Mar 2007
Posts: 27
Script to replace :
[root@unixguy ~]# cat replace.sh
#!/bin/bash
read oldval
read newval
while read line
do
{
sed 's/'$oldval'/'$newval'/g' $line >$line".new"
}
done<list.txt


Script I ran
[root@unixguy ~]# ./replace.sh
home-world
sriram
sed: can't read /home/sriram/test: No such file or directory
./replace.sh: line 3: $line".new": ambiguous redirect
[root@unixguy ~]# cat test
jshsyh hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js
jshsyh hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js
home-world hshhshsh kkkshshhshsh kjsjjsjsj ksjsjj js

can u advice where i am going wrong ???
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0