Replace multiple strings of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace multiple strings of a file
# 1  
Old 05-27-2014
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 or less entries i.e it varies)
Code:
hello sorry
yellow green

However, in output.txt i need to replace all "hello" with "sorry" and "yellow" with "green" and save all the changes in a new filename "output_new.txt".

output.txt
Code:
hello, there were two colors yellow and green.

Desired Result:
Code:
sorry, there were two colors green and green

Can you please help.
# 2  
Old 05-27-2014
This is very rough code in python idle

Code:
>>> text="""hello sorry
yellow green
""".splitlines()
>>> mapping = {}
>>> for pair in text:
	(key, val) = pair.split(" ")
	if not key in mapping.keys:
		mapping[key] = val
>>> mapping
{'hello': 'sorry', 'yellow': 'green'}
>>> strr = "hello, there were two colors yellow and green"
>>> for k in mapping.iterkeys():
	strr.replace(k, mapping[k])

'sorry, there were two colors yellow and green'
'hello, there were two colors green and green'
>>>

# 3  
Old 05-27-2014
Error

Quote:
Originally Posted by zedex
This is very rough code in python idle
Sorry !! I don't use python. Need a shell code for

SunOS mymac 5.10 Generic_150400-09 sun4v sparc SUNW,SPARC-Enterprise-T5220

Last edited by mohtashims; 05-27-2014 at 10:49 AM..
# 4  
Old 05-27-2014
With over 300++ posts already, please let's know where exactly you're stuck.....
# 5  
Old 05-27-2014
Quote:
Originally Posted by vgersh99
With over 300++ posts already, please let's know where exactly you're stuck.....
I use a for loop to traverse through the list of string from vari.txt

I am able to use sed command to replace one string and save it in a new file. I then give this new file as an input to sed for the next replacement string.

So, for 4 replace strings i have to create 4 new files. This is what i wish to avoid.

I need all the strings replaced and only one new file to be created.

Any easy way ?
# 6  
Old 05-27-2014
Code:
$ cat vari.txt 
hello sorry
yellow green

Code:
$ cat output.txt 
hello, there were two colors yellow and green.

Code:
$ awk 'FNR==NR{A[$1]=$2;next}{for(i in A)gsub(i,A[i])}1' vari.txt output.txt 
sorry, there were two colors green and green.

Use nawk on sunos
This User Gave Thanks to Akshay Hegde For This Post:
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

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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

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. while read line do field=`echo $line | awk '{print $1}'` ... (9 Replies)
Discussion started by: singh.chandan18
9 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