Connecting Tech Pros Worldwide Help | Site Map

Unable to match a backslash inside system command

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#1: Mar 10 '09
I will boil down my exact requirement to this: I should print out lines that do not contain semi-colon, backslash and closing parentheses. The following one -liner works fine.
Expand|Select|Wrap|Line Numbers
  1. perl -ne "unless(/[\\);]/) {print}" in.txt > out.txt
  2.  
Consider the following sample data:
Expand|Select|Wrap|Line Numbers
  1. Msg_create(….); \
  2. PSLogI18N\
  3. Free( ….) 
  4. eweqwdeqwe;
  5. erwerwfrrfreer)
  6. rwerttrwerterter\
  7. rtwert)
  8. et
  9. er
  10. te
  11. t
  12. wwwwwwwwwwwwwww\\
  13. erwerrwerr  ;
  14.  
But if I use the same one-liner inside a system() command in a script, backslash(\) will not be matched by the regex. Lines containing backslash will be printed out to out.txt.
Expand|Select|Wrap|Line Numbers
  1. system("perl -ne \"unless(/[\\);]/) {print}\" in.txt > out.txt");
  2.  
This is quite puzzling! Even I tried putting backslash within \Q and \E and using reverse quotes and exec() instead of system command. Got the same results :(
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Mar 10 '09

re: Unable to match a backslash inside system command


try with 4 backslashes:

[\\\\);]

My thinking is that you have to escape two of them for the shell first, then you end up with [\\);] for perl to work with.
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#3: Mar 10 '09

re: Unable to match a backslash inside system command


Yes, you are right. It worked :)

Thanks,
Nithin
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Mar 10 '09

re: Unable to match a backslash inside system command


ahhhh..... lucky guess then. You're welcome. Kind of odd though to use a perl script to run a perl one-liner from the shell. Why not just write it all in the perl script?
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#5: Mar 11 '09

re: Unable to match a backslash inside system command


In my script I would be parsing hundreds of files. The input and output filenames would be variables. I prefer writing it all in the perl script, but used this approach to reduce few lines (open & close statements). Came across this issue and wanted to know why..
Reply