Script to multiple find and replace in a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to multiple find and replace in a single file
# 1  
Old 04-20-2010
Script to multiple find and replace in a single file

Dear all

I need a script for multiple find and replace in a single file.
For example input file is -

qwe wer ert rty tyu
asd sdf dgf dfg fgh
qwe wer det rtyyui
jhkj ert asd asd dfgd

now

qwe should be replace with aaaaaa
asd should be replace with bbbbbbbb
rty should be replace with cccccc
tyu should be replace with uuuuu

There are more than 500 find and replace command. Please help me.
# 2  
Old 04-20-2010
sed search and replace ... should be easy ...
# 3  
Old 04-20-2010
I believe the find and replace commands are recorded in a file : How does it look like?
# 4  
Old 04-20-2010
Create a reference file like:
Code:
qwe aaaaaa
asd bbbbbbbb
rty cccccc
tyu uuuuu

and try this command:
Code:
awk 'NR==FNR{a[$1]=$2;next} {
  for(i=1;i<=NF;i++) {
    if(a[$i]){$i=a[$i]}
  }
}1' referencefile mainfile

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 04-20-2010
Quote:
Originally Posted by frans
I believe the find and replace commands are recorded in a file : How does it look like?
How? please explain?
# 6  
Old 04-20-2010
Quote:
Originally Posted by wildhorse
How? please explain?
Look at Franklin52 's post above...
# 7  
Old 04-20-2010
Thank you Franklin52

its working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to access multiple file and operate on single file

We have three files as mentioned below: 1. main_file.txt: This is the file in which all operations will be done. Which means this file will be signed by using the below two files 2. signature_file.txt: This is a status file and contains two signatures. 3. command.txt:file contains two commands... (2 Replies)
Discussion started by: chetanojha
2 Replies

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

3. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Emergency UNIX and Linux Support

Find, replace, file path in multiple files for Solaris 10

Guys I have a big issue that I need to get fixed ASAP however I can not seem to find a way to do it. We started to use zones with Solaris 10 at work and we moved a zone from a SIT box to a DEV box. Problem is the software we have installed is looking at a /lcl/sit/apps/ path and it needs to look... (5 Replies)
Discussion started by: LRoberts
5 Replies

6. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

7. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

8. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

9. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

10. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies
Login or Register to Ask a Question