Vadym Vadym - 2 months ago 14
Bash Question

sed system command in perl script

I am trying to put the system command like below to the perl script, but
sed expression contains both quotes and backticks and I am not sure how to escape all of them, so it will execute my system command exactly as I need.

Here is the example of the command:

mysql -u root -D porta-billing -e "..." | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"


The answer to the question you're asking is to use the qx(...) operator. qx(...) is the "choose your own delimiter" version of backticks.

my $output = qx[ ... ];


my $output = qx( ... );


my $output = qx! ... !;

It's easy to find a delimiter that won't clash with the characters in your command string.

But the answer to the question that you should be asking has two parts:

  • Don't call mysql from your Perl program - use DBI instead.
  • Don't call sed from your Perl program - use Perl code to manipulate your text.

I feel slightly nervous about the first part of my answer as I'm worried you will just take my hacky workaround and end up with an unmaintainable mess. Please take note of the advice in the second half - even if you ignore it in this case.