using bash command in perl
i have short b开发者_运维技巧ash code
cat example.txt | grep mail | awk -F, '{print $1}' | awk -F= '{print $2}'
I want to use it in perl script, and put its output to an array line by line. I tried this but did not work
@array = system('cat /path/example.txt | grep mail | awk -F, {print $1} | awk -F= {print $2}');
Thanks for helping...
The return value of system()
is the return status of the command you executed. If you want the output, use backticks:
@array = `cat /path/example.txt | grep mail | awk -F, {print \$1} | awk -F= {print \$2}`;
When evaluated in list context (e.g. when the return value is assigned to an array), you'll get the lines of output (or an empty list if there's no output).
Try:
@array = `cat /path/example.txt | grep mail | awk -F, {print \$1} | awk -F= {print \$2}')`;
Noting that backticks are used and that the dollar signs need to be escaped as the qx operator will interpolate by default (i.e. it will think that $1 are Perl variables rather than arguments to awk).
Couldn't help making a pure perl version... should work the same, if I remember my very scant awk correctly.
use strict;
use warnings;
open A, '<', '/path/example.txt' or die $!;
my @array = map { (split(/=/,(split(/,/,$_))[0]))[1] . "\n" } (grep /mail/, <A>);
精彩评论