Results 1 to 5 of 5
I have the following code, which replaces for a particular string. I have had to escape already the characters with \ to find \, for example. The sed statement is ...
- 02-12-2009 #1Just Joined!
- Join Date
- Jan 2006
- Posts
- 15
Perl and Sed
I have the following code, which replaces for a particular string. I have had to escape already the characters with \\ to find \, for example. The sed statement is working fine and woks on the command line with no problems for replacing the desired string. However when I attempt to perform with the following script, I get the following problem:
Unrecognized escape \s passed through at ChangeLegends.pl line 20.
Illegal hexadecimal digit '/' ignored at ChangeLegends.pl line 20.
Missing braces on \N{} at ChangeLegends.pl line 20, within string
Execution of ChangeLegends.pl aborted due to compilation errors.
Even when calling from within the script, not using the command I get:
Bareword found where operator expected at ChangeLegends.pl line 19, near "s/k\\\sB\\\NT\\/\\\xe/M"
Unquoted string "agr" may clash with future reserved word at ChangeLegends.pl line 19.
Unquoted string "agx" may clash with future reserved word at ChangeLegends.pl line 19.
Missing braces on \N{} at ChangeLegends.pl line 19, within pattern
syntax error at ChangeLegends.pl line 19, near "s/k\\\sB\\\NT\\/\\\xe/M"
Execution of ChangeLegends.pl aborted due to compilation errors.
How should I go about performing this simple operation. Thanks
Code:#!/usr/bin/perl -w $command = "find . -name '*.agr' > plot_agr.list\n"; system($command); open(LISTFILE,"plot_agr.list") or die "File plot_agr.list not found! $!\n"; print "Files to plot:\n\n"; while (<LISTFILE>) { chomp; s/\s//g; $figures[$#figures+1]= $_; print $#figures+1,". $_\n"; $name = $_; $name =~ s/.agr//; #s/k\\\sB\\\NT\\/\\\xe/M/g $name.agr > $name.agx; $command = "sed -e s/k\\\sB\\\NT\\/\\\xe/M/g $name.agr > $name.agx\n"; system($command); } close(LISTFILE); print "\njob complete.\n";
- 02-12-2009 #2Just Joined!
- Join Date
- Oct 2004
- Posts
- 62
I don't know Perl (I use Python) but I know Bash...
The sed expression;
should not be written...?Code:$command = "sed -e s/k\\\sB\\\NT\\/\\\xe/M/g $name.agr > $name.agx\n";
Telling the truth... I didn't catch the problem...Code:$command = "sed -e 's/k\\\sB\\\NT\\/\\\xe/M/g' $name.agr > $name.agx\n";
In these cases its better to include a very small example of your input and of what you are expecting to be the output....
Bye.
- 02-12-2009 #3Just Joined!
- Join Date
- Jan 2006
- Posts
- 15
Well the sed expression works, but for those who want to see what it is doing.
Its is replacing the string "k\sB\NT/\xe" with "M". Of course I have had to employ escape characters to make it work. I suspect it is these escape characters which are causing the headache.
- 02-12-2009 #4Just Joined!
- Join Date
- Jan 2006
- Posts
- 15
Using escape characaters for perl i.e. "\" and part of the string to replace
string to replace with "M"
"k\\sB"
Using
"$command = "sed -e 's/k\\sB/M/g' $name.agr > $name.new";"
Just does nothing, but does not return any errors when parsing. Any ideas?
- 02-12-2009 #5Linux User
- Join Date
- Jun 2007
- Posts
- 318
Try this:
First notice that I put a print command in before the system command so you can see the actual command being executed in the shell. That helped in debugging your problem.Code:$command='sed -e \'s|k\\\\sB\\\\NT/\\\\xe|M|g\' $name.agr > $name.new'; print "$command\n"; system($command);
Next I used "|" instead of "/" as the delimiter in sed to simplify the string.
Now why (I think) my solution works. The string is being parsed twice. Once by perl and once by sed. So when perl executes the $command=... the '\\\\' gets reduced to '\\' so resulting string is:
When sed executes the command the '\\' gets reduced to '\' so the substitute command looks like:sed -e 's|k\\sB\\NT/\\xe|M|g' $name.agr > $name.new
And that gives you what you want.'s|k\sB\NT/\xe|M|g'


Reply With Quote