Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl / Awk - Strange Result for 1 liner

Status
Not open for further replies.

AntP

Programmer
Apr 12, 2002
6
US
hope I don't get killed for potentially posting under the wrong forum. :eek:) I did think hard about where this belongs...

Basically I have a simple line of code which is not producing what I expect when I run it from a perl script.

It does however work when run directly from the prompt:
fgrep "ERROR" file_output.txt | nawk ' { print $2 } '
This produces what I'd expect:
(Failed
(Failed

However when I run from the pl script it returns the entire string, and not the "2nd field". Here's the simplified perl script:
#!/sbcimp/run/pd/perl/prod/bin/perl

$temp_txt=`fgrep "ERROR" file_output.txt | nawk ' { print $2 } '`;
print $temp_txt;

File content is as follows:
ERROR1 (Failed to match)
SUCCESS (Matching)
ERROR2 (Failed to update)

Can anyone give me any pointers please? I've stared at this for ages now, I know I am being thick, and the solution is more than likely real simple, but hey....
Thanks a bundle.
 
Code:
$temp_txt=`fgrep "ERROR" file_output.txt | nawk ' { print \$2 } '`;

It seems perl was interpolating the $2 on it's own. Normally it doesn't on things within single quotes, but I suppose the backticks canceled that out or something. Who knows. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Many thanks, worked a treat. I can sleep in peace now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top