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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk inside perl

Status
Not open for further replies.

25884

Programmer
Mar 12, 2001
46
0
0
AP
Hi,

I need to execute the following command inside perl :

grep string file_cont_string | awk -F ' ' '{print $3}'

How do i do that??

TIA
 
Rather than shelling out to two piped commands, it's probably easier to do the whole thing in perl.

Correct me if I'm wrong (I stopped using awk after I discovered perl), but it seems like you are looking for lines in a file that match some string, and collecting the third field from each found line?
Code:
use strict;
use warnings;

while (<>) {
   print split($_)[3], "\n" if (/string/);
}
Might do it. (Untested)

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Mmm. Should be [2] not [3] in the above... [blush]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Or, as a one liner:

Code:
perl -n -e "/string/ && print \"\" . (split \" \", $_)[2] . \"\n\"" file_cont_string
 
Alternatively:
Code:
perl -ane '/string/ && print "$F[2]\n"' file_cont_string
 
Thanks ishnid.

I knew there was a way to do it more compactly, but my coffee hasn't quite sunk in yet. [morning]
 
If you really have to use, the you might try this:

Code:
$data = `grep string file_cont_string | awk -F ' ' '{print \$3}'`;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top