Find and replace, perl code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace, perl code
# 1  
Old 07-27-2013
Find and replace, perl code

Hi,

Can somebody tell whats wrong with "find and replace perl code".

I wanted to find "\n".write(status,data" and replace with "\n",data" in multipls files.


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


foreach $fileName (@files) {
  print "$fileName\n";
 
  my $searchStr0 = '\n".write(status,data';
  my $replaceStr0 = '\n",data';

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


thx in advance

Rgds,
Ravi
# 2  
Old 07-27-2013
Post a block of sample data from one file with a couple of lines before and after the lines where the replacement should occur.
# 3  
Old 07-27-2013
Hi,

I want to find and replace following highlighted portion

Code:
$display("Register write value\n".write(status,data));

with following.

PHP Code:
$display("Register write value\[B]n",data[/B]); 
Rgds,
Ravi.

Last edited by chettyravi; 07-28-2013 at 01:48 AM..
# 4  
Old 07-28-2013
Quote:
Originally Posted by chettyravi
...
I want to find following line
Code:
$display("Register write value\n".write(status,data));

with this line.
PHP Code:
$display("Register write value\n",data); 
...
Code:
$
$ cat file1.c
file1.c - line 1
file1.c - line 2
$display("Register write value\n".write(status,data));
file1.c - line 4
file1.c - line 5
$
$ perl -plne 's/\.write\(status,data\)/,data/' file1.c
file1.c - line 1
file1.c - line 2
$display("Register write value\n",data);
file1.c - line 4
file1.c - line 5
$

# 5  
Old 07-28-2013
thx u durden_tyler.

its working.

Rgds,
Ravi.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. UNIX for Dummies Questions & Answers

Perl find & replace - what am I doing wrong?

Hi! I have a directory full of .plist type files from which I need to delete a line. Not every file contains the line, but of course I'd like to do it recursively. The line which I want to delete is: <string>com.apple.PhotoBooth</string> and looks like this in its native habitat: ... (9 Replies)
Discussion started by: sudon't
9 Replies

3. Shell Programming and Scripting

Character Find and replace in Unix or Perl

Hi, I am stuck with an problem and want some help, what i want to do is There is a directory name temp which include file named t1.txt, t2,txt, t3.txt and so on. These files contains data, but there are some bad character also that is % present in the files , I want to write the script... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

4. Shell Programming and Scripting

Have a find/replace perl script thats broke

Hello Folks, #!/usr/bin/perl use File::Find; open F,shift or die $!; my %ip=map/(\S+)\s+(\S+)/,<F>; close F; find sub{ if( -f ){ local @ARGV=($_); local $^I=""; while( <> ){ !/#/ && s/(\w+)\.fs\.rich\.us/$ip{$1}/g; print; } }... (8 Replies)
Discussion started by: richsark
8 Replies

5. Shell Programming and Scripting

perl find and replace

Hi, Please could someone advise, how I can resolve this issue with my find and replace command : perl -i -npe "s#RLM_LICENSE.*#RLM_LICENSE=$TEAM_TOP/licenses/abc.demo.lic#;" environment.properties $TEAM_TOP is an environment varible within my system. when i run this perl command from... (1 Reply)
Discussion started by: fettie
1 Replies

6. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

7. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

8. Shell Programming and Scripting

Find/replace to new file: ksh -> perl

I have korn shell script that genretaets 100 file based on template replacing the number. The template file is as below: $ cat template file number: NUMBER The shell script is as below: $ cat gen.sh #!/bin/ksh i=1; while ((i <= 100)); do sed "s/NUMBER/$i/" template > file_${i} ((... (1 Reply)
Discussion started by: McLan
1 Replies

9. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies

10. UNIX for Dummies Questions & Answers

Find and Replace code help needed

I have a file "dbshot.xml" that contains lines that need replacing in a batch format but the parameters are based on two lines. Ex. <role roletype="01"> <status>1 needs to be changed to <role roletype="01"> <status>0 I can't use simply "<status>1" replace since the... (2 Replies)
Discussion started by: E Orgill
2 Replies
Login or Register to Ask a Question