Replace multiple strings in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace multiple strings in a file.
# 1  
Old 12-07-2010
Replace multiple strings in a file.

Hello Freinds,

Hope, you all are doing well.

I have to replace the static strings from one file (File 1) with the dynamic strings from the another file (File2). I've written a shell script for this.Below is the contents.
HTML Code:
 while read line
do
 field[0]=`echo $line | awk '{print $1}'`
 field[1]=`echo $line | awk '{print $2}'`
 field[2]=`echo $line | awk '{print $3}'`
  sed -e "s/Contact_FirstName/`echo ${field[0]}`/g;s/Contact_LastName/`echo ${field[1]}`/g;s/Email_ID/`echo ${field[2]}`/g" File1 > File3
done < File2
~
.

I am using both awk and sed to serve my purpose and the script is working as expected.

I just wanted to know if there is any better way to achieve this.

Please suggest!!

Thanks,
Chandan Singh
# 2  
Old 12-07-2010
Code:
cut -f1,2,3 File2 > File3

will also create the same output. please specify if your output is something else.
R0H0N
# 3  
Old 12-07-2010
Hi Rohon,

No, this is not what I wanted.

I need to search the following strings in File1 and replace them with the contents in File2 and the output will be redirected to File3, which will contain all the contents from File1 with the replaced strings.
HTML Code:
Contact_FirstName
Contact_lastName
Email_ID
The content of the File2 is
HTML Code:
Chandan Singh chandan.singh@example.com
Desired output is something like this
Code:
 #cat File3 | egrep 'chandan|singh'
                        <per:firstName>chandan</per:firstName>
                        <per:lastName>singh</per:lastName>
                        <per:email>chandan.singh@example.com</per:email>
                        <


Last edited by singh.chandan18; 12-07-2010 at 10:39 AM..
# 4  
Old 12-07-2010
Code:
<File2 read a b c
sed 's/Contact_FirstName/'"$a"'/;s/Contact_lastName/'"$b"'/;s/Email_ID/'"$c"'/' File1 >output

Code:
# cat File1
Contact_FirstName
Contact_lastName
Email_ID
# cat sampl
Chandan Singh chandan.singh@example.com
# <sampl read a b c
# sed 's/Contact_FirstName/'"$a"'/;s/Contact_lastName/'"$b"'/;s/Email_ID/'"$c"'/' File1
Chandan
Singh
chandan.singh@example.com


Last edited by ctsgnb; 12-07-2010 at 11:28 AM..
# 5  
Old 12-07-2010
not sure , if there are only 3 column in File2, if not,

Code:
while read line
do
 set -- $(echo $line|awk '{print $1,$2,$3}')
 sed -e "s/Contact_FirstName/$1/g;s/Contact_LastName/$2/g;s/Email_ID/$2/g" File1 >> File3
done < File2

# 6  
Old 12-08-2010
@rdcwayx

can you pls tell wot the below line mean..
Code:
set -- $(echo $line|awk '{print $1,$2,$3}')

# 7  
Old 12-08-2010
I m sorry Chandan..!!! I didn't understand your question properly.

You can achieve this by following command also. This will directly replace values in File1 itself.

Code:
perl -pi -e 's/Contact_FirstName/`cat File2 | cut -f1`/g;s/Contact_LastName/`cat File2 | cut -f2`/g;s/Email_ID/`cat File2 | cut -f3`/g' File1

Sorry again..!!
R0H0N
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Replace multiple strings of a file

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. vari.txt (in this sample we have 2 entries, but it can have more... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Grep and replace multiple strings in a file with multiple filenames in a file

Hi, I have a file containing list of strings like i: Pink Yellow Green and I have file having list of file names in a directory j : a b c d Where j contains of a ,b,c,d are as follows a: Pink (3 Replies)
Discussion started by: madabhg
3 Replies

4. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 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

Replace the strings in file

Hello members, I been following this forums since very long time. I need to do one job. In my script I am evaluating one variable, lets say n=100. Now i have xml file inside which i need to replace the numbers in the desired lines with the evaluated number(n) +1. For example let's say... (4 Replies)
Discussion started by: mailtosaiki
4 Replies

7. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

8. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

replace strings in a file

Hi I want to change the following passwd: files nis group: files nis in /etc/nsswitch.conf to be passwd: files compat group: files compat I tried cp -p nsswitch.conf nsswitch.conf.old (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question