Visit Our UNIX and Linux User Community

multiple input search and replace script

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers multiple input search and replace script
# 1  
Old 03-21-2007
multiple input search and replace script

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"

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  
Old 03-22-2007
For your qn1,
below one will be a solution...

read oldval
read newval
while read line
sed 's/'$oldval'/'$newval'/g' $line >$line".new"

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  
Old 03-22-2007
For q2, GNU sed has -i option
# 4  
Old 04-29-2007
Script to replace :
[root@unixguy ~]# cat
read oldval
read newval
while read line
sed 's/'$oldval'/'$newval'/g' $line >$line".new"

Script I ran
[root@unixguy ~]# ./
sed: can't read /home/sriram/test: No such file or directory
./ 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 ???

Previous Thread | Next Thread
Test Your Knowledge in Computers #587
Difficulty: Medium
All programming languages support recursion.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

Search a template file and replace with input

Hi I have a CommonTemplateStop.template file . Inside the file i need to replace the variables DepName and CompInsName with the values(Trade and TradeIns) specified in the script. I have written the below .sh script in linux server which will read the .template file and has to replace the 2... (8 Replies)
Discussion started by: samrat dutta
8 Replies

2. Shell Programming and Scripting

Search and replace multiple patterns in a particular column only - efficient script

Hi Bigshots, I have a pattern file with two columns. I have another data file. If column 1 in the pattern file appears as the 4th column in the data file, I need to replace it (4th column of data file) with column 2 of the pattern file. If the pattern is found in any other column, it should not... (6 Replies)
Discussion started by: ss112233
6 Replies

3. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

4. Shell Programming and Scripting

Search and Replace in multiple files

Hello, I have hundreds of files in which I need to change email address. Here is what I am trying to do: 1. All text files are in a directory "a" 2. In the text file, I want to replace email address for preparer. All these lines start with {{PreparerEmail and end with }}. The email... (3 Replies)
Discussion started by: cartrider
3 Replies

5. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

6. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

7. Shell Programming and Scripting

help with search and replace in multiple fields

I have a pipe delimited file with 27 fields. Each record has 26 fields. I need to search for the 25,26,27 fields and replace "," with nothing. How can I acheive this. Sed is more preferred. e.g data row o/p (5 Replies)
Discussion started by: dsravan
5 Replies

8. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

9. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

10. UNIX for Dummies Questions & Answers

Multiple search and replace

Hi, I have different files and I want to automatically change the values of the defined variables. i want to get rid of editing all the files and make it a little bit faster. my problem is like this. 1. i will input all the new values. 2. substitute this values into the values inside the... (1 Reply)
Discussion started by: tungaw2004
1 Replies

Featured Tech Videos