0

How I can make the following external command within ticks work with variables instead?

Or something similar?

sed -i.bak -e '10,16d;17d' $docname; (this works)

I.e., sed -i.bak -e '$line_number,$line_end_number;$last_line' $docname;

1
  • Could try this: system(qq{sed -i.bak -e '$line_number,$line_end_number;$last_line' $docname}) Commented Mar 12, 2012 at 20:17

3 Answers 3

3
my $result = 
      qx/sed -i.bak -e "$line_number,${line_end_number}d;${last_line}d" $docname/;

Where the line split avoid the horizontal scroll-bar on SO; otherwise, it would be on one line.

Or, since it is not clear that there's any output to capture:

system "sed -i.back '$line_number,${line_end_number}d;${last_line}d' $docname";

Or you could split that up into arguments yourself:

system "sed", "-i.back", "$line_number,${line_end_number}d;${last_line}d", "$docname";

This tends to be safer since the shell doesn't get a chance to interfere with the interpretation of the arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

I advise always using the multi-argument form of Perl's system() function, unless you have a specific requirement to have the shell parse some of the arguments (I/O redirection. etc.)
0
@args = ("command", "arg1", "arg2");
system(@args) == 0 or die "system @args failed: $?"

Furthermore on the manual:

perldoc -f system

Comments

0

I think you should read up on using qq for strings.

You probably want something like this:

use strict;
use warnings;

my     $line_number = qq|10|;
my $line_end_number = qq|16d|;
my       $last_line = qq|17d|;
my        $doc_name = qq|somefile.bak|;
my     $sed_command = qq|sed -i.bak -e '$line_number,$line_end_number;$last_line' $doc_name;|;

print $sed_command;
qx|$sed_command|;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.