Justin Justin - 1 year ago 100
Perl Question

Escaping in bash within perl

I'm running something in perl and have the following command, which deletes consecutive duplicate lines (only keeping one of them)

system("sed -i '$!N; /^\(.*\)\n\1$/!p; d' *[13579].csv");

However, when I run this, I get the following error:

sed: -e expression #1, char 11: unterminated address regex

I have a feeling it has to do with my escaping, but I'm not too certain as I am rather inexperienced with perl and bash. I know the dollar signs should be escaped, but what about the backslashes? Does anyone have a good resource they can point me to to learn more about escaping bash within perl? Thanks!

Answer Source

When putting sed in Perl it can be fussy, there's a couple things you could do. The first would be to change the type of quotes you wrap around the command system is running and the sed pattern (flip outer to single, inner to double); the other option would be to escape the \ characters in sed.

system('sed "$!N;/^\(.*\)\n\1$/!p;d" *[13579].csv');

Escaping would be something like this:

system("sed '$!N;/^\\(.*\\)\\n\\1\$/!p;d' *[13579].csv");

Edit: (If your file name happens to include spaces then those would need escaping as well):

system("sed '$!N;/^\\(.*\\)\\n\\1\$/!p;d' file\\ with\\ spaces\\ *[13579].csv");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download