By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Unable to match a backslash inside system command

nithinpes
Expert 100+
P: 410
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 :(
Mar 10 '09 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Mar 10 '09 #2

nithinpes
Expert 100+
P: 410
Yes, you are right. It worked :)

Thanks,
Nithin
Mar 10 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
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?
Mar 10 '09 #4

nithinpes
Expert 100+
P: 410
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..
Mar 11 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.