0

I am running the below code in my perl script.

my $record = `awk -F'[:;]' '$1 == "Amy"' data.txt`;

However, it's giving me the error:

awk: syntax error at source line 1
 context is
     >>>  == <<< 
awk: bailing out at source line 1

What is causing this error?

1
  • untested, my ($record) = map scalar(qx($_)), q(awk -F'[:;]' '$1 == "Amy"' data.txt); Commented Mar 7, 2017 at 11:55

2 Answers 2

2

Try to escape the $ sign:

my $record = `awk -F'[:;]' '\$1 == "Amy"' data.txt`;
Sign up to request clarification or add additional context in comments.

Comments

1

Calling awk in backticks from perl is a pretty nasty thing to do. perl replicates pretty much all the same functionality - all you do is introduce additional overhead, inefficiency and quoting problems (like you've got in your example).

Why not instead:

open ( my $input, '<', 'data.txt' ) or die $!;
my ($record) = grep { (split /[:;]/)[0] eq 'Amy' } <$input>; 

This replicates what you're doing, but you could instead do something altogether more elegant like:

my %person;
while ( <$input> ) { 
    chomp;
    my ( $name, @fields ) = split /[;:]/; 
    $person{$name} = \@fields; 
}

And then:

print join " ", @{$person{'Amy'}},"\n";

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.