Or you can put parenthesis around the part that you want to extract, and reference them by $1, $2, etc. Like this:if (/^\S+/) { print "$&\n"; }
Both of the cases above extract any non-whitespace characters at the beginning of the line. Although if that's all you want to do, you are probably better off using split as suggested by others in this thread. It's simpler and probably more efficient. Use regular expressions if you only want to match certain lines that satisfy certain conditions.if (/^(\S+)\s+/) { print "$1\n"; }
So to strictly emulate the shell/awk command line you gave, you can use this:
The -a flag causes it to automatically split each line into whitespace-separated fields, leaving the result in the @F array. The -n option puts a while(<>) { ... } loop around the code. The code itself is specified by the -e option.xcommand | perl -nae 'print $F[0],"\n"'
From you question, it seems as if you want to provide the command to execute as input to the program. In that case you could do something like this:
my $command="xcommand"; open(CMD, "$command |") or die "Error: $!\n"; while(<CMD>) { print (split(" "))[0]."\n"; # or whatever else } close(CMD);
--ZZamboni
In reply to RE: RE: Re: Help for awk/regex/newbie
by ZZamboni
in thread Help for awk/regex/newbie
by cmenser
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |