The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2007
tungaw2004 tungaw2004 is offline
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.
  #2 (permalink)  
Old 03-22-2007
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 566
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...
  #3 (permalink)  
Old 03-22-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
For q2, GNU sed has -i option
  #4 (permalink)  
Old 04-29-2007
sriram003 sriram003 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 33
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 ???
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0