Parentheses in perl find/replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parentheses in perl find/replace
# 1  
Old 08-19-2007
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

Code:
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 "G(d)" with "G('d,'p)."

The interpreter seems to just ignore the parentheses and not finding the string "Gd" simply does nothing. I can't just find/replace "d" as the character appears throughout the file.

Any help?
# 2  
Old 08-19-2007
What is the exact regular expression that you applied? It was likely not correct.
# 3  
Old 08-20-2007
"G(d)" matches the first occurance of string "Gd" and puts "d" in $1. To match string "G(d)" AS IS, use "G\(d\)"
# 4  
Old 08-21-2007
Thanks to everyone for the help.

The regex I was using was incorrect, but a little reading took care of that.
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. Shell Programming and Scripting

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. #!/usr/bin/perl my @files = <*.c>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr0 =... (4 Replies)
Discussion started by: chettyravi
4 Replies

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

4. Shell Programming and Scripting

Sed command to replace with pattern except for text and closing parentheses

Can someone help me with a sed command: There will be multiple occurences in a file that look like this: MyFunction(12c34r5) and I need to replace that with just the 12c34r5 for every occurrence. The text between the parentheses will be different on each occurrence, so I can't search for that.... (4 Replies)
Discussion started by: missb
4 Replies

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

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

7. UNIX for Dummies Questions & Answers

Replace all occurrences of strings with parentheses

Hi, I tried to adapt bartus's solution to my problem, without success. I want to replace all the occurences of this: with: , where something can contain an arbitrary number of balanced parens and brakets. Any ideas ? Best, (1 Reply)
Discussion started by: ff1969ff1969
1 Replies

8. Shell Programming and Scripting

Perl - Parentheses & Context confusion

Hi Expert, Could you please explain why below two perl code get different result? Thanks a lot. sub test{ return (2,3,4,5,6,3,4,50); } ($a,$b)=(test); # 3,6 ($a,$b)=test; # 2,3 (2 Replies)
Discussion started by: summer_cherry
2 Replies

9. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

10. Shell Programming and Scripting

Replace text in parentheses

Hi I would like to replace a comma in parentheses to a semicolon for example. Other commas outside () stay unchanged. How can I do this? aaaa,bbb,ccc,ddd(eee,fff,ggg),hhh,iii to aaaa,bbb,ccc,ddd(eee;fff;ggg),hhh,iii Thanks (5 Replies)
Discussion started by: lalelle
5 Replies
Login or Register to Ask a Question