ZebraSocks
ZebraSocks

Reputation: 85

Why doesn't the Unix command work in the Perl script?

I want to extract part of a text file starting at a certain pattern and store it in another file. My Perl script takes a single argument as the input text file. So far, I have:

my $INPUT = $ARGV[0];
my $LINES_TO_DUMP = 4000;
my $startline = `egrep -n "^PATTERN" $INPUT | head -1 | cut -f1 -d:`;

# THIS LINE DOESN'T WORK
system("tail +$startline $INPUT | head -$LINES_TO_DUMP > extracted.txt");

When I run my program, it gets 'stuck' running the second command (the egrep command works, and stores the correct number). I've run the command in my terminal to make sure it works.

What is going wrong?

Upvotes: 2

Views: 120

Answers (1)

razor
razor

Reputation: 110

It's likely that your $startline has a newline in it, as it's consumed from command output. You should confirm this and then use chomp() on $startline prior to your system() call.

Here's the perldoc for chomp as suggested.

Upvotes: 5

Related Questions