JDY
JDY

Reputation: 167

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!

Upvotes: 1

Views: 177

Answers (1)

l'L'l
l'L'l

Reputation: 47302

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 -i "$!N;/^\(.*\)\n\1$/!p;d" *filename');

Note: since your filename uses a special characters there might be escaping needed for that to work with globbing (eg. *\\[13579].csv); escaping would be something like this:

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

If your file name happens to include spaces then those would need escaping as well.

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

sed would then find any files matching *[12345] [13579].csv and in-place edit them.

Upvotes: 1

Related Questions