Perl/sed Escape Syntax Problem . . .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl/sed Escape Syntax Problem . . .
# 1  
Old 10-30-2014
Perl/sed Escape Syntax Problem . . .

Greetings!

I've run into this before; and am having a spot of trouble trying to figure out the way that Perl would prefer the following example syntax to be formed:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

`sed -i 's/Hi Mom!\|Hi Dad!/Bye Everyone!/I' ./test.txt`;

Perl doesn't throw a fit; but sed simply looks at you funny and does nothing at all...

So, while there are "sleeker" ways of handling this with pure perl, this type of issue has bugged me for a bit. Any ideas as to how one might pull things together syntaxwise to make it tick over???

Smilie

Thanks for the help, and have a great day!
# 2  
Old 10-30-2014
Try:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

`sed -i 's/Hi Mom!\\|Hi Dad!/Bye Everyone!/I' ./test.txt`;

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 10-30-2014
You need two \\ because...
perl passes one \ to a shell,
the shell finds the \ within a 'string' and leaves it unchanged,
sed finds \|
Bingo.
If the shell would find the \ outside a 'string' it would quote the next character, so you would need \\\\ in your perl code -> Perl passes \\ to the shell -> the shell passes \ to sed -> sed finds \|
You should really do that in Perl...
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-31-2014
Thanks, folks, for the clarifications and input.
Quote:
If the shell would find the \ outside a 'string' it would quote the next character, so you would need \\\\ in your perl code -> Perl passes \\ to the shell -> the shell passes \ to sed -> sed finds \|
You should really do that in Perl...
Thanks, MadeInGermany, for the smile. I do agree; but I also truly love Monty Python Smilie

Thanks again; and have a great weekend!
# 5  
Old 11-04-2014
You can also do something like this:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

qx'sed -i "s/Hi Mom!\|Hi Dad!/Bye Everyone!/I" ./test.txt';

No more double backslash, but you have to change the quote character in the sed command.
This User Gave Thanks to bartus11 For This Post:
# 6  
Old 11-04-2014
@bartus11:

Interesting and helpful!

What are we doing here; and what are the pros & cons of using this approach???

Smilie
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 escape % with sed?

Hello, I am running ubuntu 16.04 I searched "how to replace dot by % using sed" but no luck. file My.Name.Is.Earl Expected output My%Name%Is%Earl I tried: sed -i "s|.||g" file sed -i "s|.|{%}|g" file sed -i "s|.|\%|g" file I'd appreciate your help Thank you Boris (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

sed - with escape character

i have string as below str=".<date>" in which i need to replace < with /< , when i tried with sed , got the output. --> echo $str | sed 's/</\\</g' .\<date> when i tried to assign it to a variable , i am not getting the same --> a=`echo $str | sed 's/</\\</g'` ; echo $a... (4 Replies)
Discussion started by: expert
4 Replies

3. Shell Programming and Scripting

Problem with using a sed to add escape character \ before $ and ' symbols

Hi all, I've got a problem with sed. Want to use it to add escape character \ before $ and ' symbols so condition='1'$some will become condition=\'1\'\$some echo "condition='1'$some" | sed 's/\($\)/\\\1/g' is not working properly. Can somebody help me with this please? Regards,... (7 Replies)
Discussion started by: johny_be_good
7 Replies

4. Shell Programming and Scripting

Problem with sed in perl!!

Hi, I am facing an issue with sed in perl. I have a file which has 2 header lines and one trailer line. I need to process the file without these headers and trailer. My file looks like : File.txt:- Header1 Header2 data1 data2 trailer For removing header and trailer from file I am using... (5 Replies)
Discussion started by: abhisharma23
5 Replies

5. Shell Programming and Scripting

perl how to escape (|) character

my @array; my $sepa = "|"; print $sepa; open FH, "<100_20091023_2.txt"; while(<FH>){ push @array, split(/\$sepa/, $_); print "@array\n\n"; } I am not able split the line which have | separated (1 Reply)
Discussion started by: pritish.sas
1 Replies

6. Shell Programming and Scripting

SED: Replacing $1 with $2 escape problem

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (7 Replies)
Discussion started by: mark007
7 Replies

7. Shell Programming and Scripting

Escape character - sed

Hi All, How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character? Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors :( Example of etc/passwd file: blah:x:1055:600:blah... (6 Replies)
Discussion started by: c00kie88
6 Replies

8. Shell Programming and Scripting

Perl syntax for sed searches

I am aware that Perl has a lot of features that originally came from sed and awk. I have a pattern that I am using like this: sed -n '/|Y|/p' I want to do the same thing in Perl and be able to either save that value in some kind of variable or array or potentially write it out to a file. ... (11 Replies)
Discussion started by: masinick
11 Replies

9. Shell Programming and Scripting

Help for Sed Syntax problem

I have one File named "txt_file" # cat txt_file <DBType>RT</DBType> <AppType>RT</AppType> -------------------------------------------------- I want replace "<AppType>RT</AppType>" to <AppType>XY</AppType> in txt_file and output redirect to Newfile ... (2 Replies)
Discussion started by: SanjayLinux
2 Replies

10. UNIX for Dummies Questions & Answers

possible to escape the \ character in sed?

is it possible to escape the \ character in sed? right now I'm trying to replace all occurances of \ with \\ sed \"s|test|test_replacement|g\" file1 > output; #this works fine sed \"s|\\|\\\|g\" file1 > output; #this generates the following error: sed: -e expression #1, char 17:... (1 Reply)
Discussion started by: gammaman
1 Replies
Login or Register to Ask a Question