Find multiple strings and replace single string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find multiple strings and replace single string
# 1  
Old 07-09-2013
Find multiple strings and replace single string

Hi,

following Perl code i used for finding multiple strings and replace with single string.

code:

Code:
#!/usr/bin/perl
my @files = <*.txt>;


foreach $fileName (@files) {
  print "$fileName\n";

  my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)';
  my $replaceStr = ".read(status,rdata)";

  open(FILE,$fileName) || die("Cannot Open File");
    my(@fcont) = <FILE>;
  close FILE;
  
  open(FOUT,">$fileName") || die("Cannot Open File");
    foreach $line (@fcont) {
      $line =~ s/$searchStr/$replaceStr/g;
      print FOUT $line;
    }
  close FOUT;
}

i am not seeing ",rdata" , ",,rdata" and ", ,rdata" replaced with ".read(status,rdata)"

I used OR operator.

Rgds,
Ravi

Last edited by radoulov; 07-09-2013 at 08:34 AM..
# 2  
Old 07-09-2013
Pls refer the thread that you posted last week.

https://www.unix.com/shell-programmin...le-string.html

Also you can use a regex instead of OR and saving to variables..

Code:
$line=~ s/,\s?,?rdata\)/\.read\(status,rdata\)/g;

# 3  
Old 07-09-2013
thx rajamadhavan,

Is this following code is correct:

Code:
Code:
$line=~ s/,\s?,?rdata\)|,,?rdata\)|,?rdata\)/\.read\(status,rdata\)/g;

ravi.

Last edited by Franklin52; 07-09-2013 at 08:53 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

2. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

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

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

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

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

7. UNIX for Dummies Questions & Answers

find single quote in a string and replace it

Hi, I have variable inside shell script - from_item. from_item = 40.1'1/16 i have to first find out whether FROM_ITEM contains single quote('). If yes, then that need to be replace with two quotes (''). How to do it inside shell script? Please note that inside shell script........ (4 Replies)
Discussion started by: yogichavan
4 Replies

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

9. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: wildhorse
6 Replies

10. UNIX for Dummies Questions & Answers

Find and replace a 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) ... (2 Replies)
Discussion started by: pharos467
2 Replies
Login or Register to Ask a Question