简体   繁体   中英

perl one liner alternative to this bash “chain”?

I am trying to comprehend Perl following the way describe in the book "Minimal Perl".

I've uploaded all source txt files onto my own server : results folder

I got the output from using several bash commands in a "chain" like this:

cat run*.txt | grep '^Bank[[:space:]]Balance'|cut -d ':' -f2 | grep -E '\$[0-9]+'

I know this is far from the most concise and efficient, but at least it works...

As our uni subject now moves onto the Perl part, I'd like to know if there is a way to get the same results in one line?

I am trying something like the following code but stuck in the middle:

Chenxi Mao@chenxi-a6b123bb /cygdrive/c/eMarket/output
$ perl -wlne 'print; if $n=~/^Bank Balance/'
syntax error at -e line 1, near "if $n"
Execution of -e aborted due to compilation errors.

you shouldn't have a ; after the print. So

perl -wlne 'print $1 if $n=~/^Bank Balance\s*:\s*(\d+)/'
perl -F/\:/ -ane 'print $F[1]."\n" if /Bank Balance/ && $F[1]!~/\$-/' run*.txt

这也是bash命令的简短版本,仅使用awk

awk  -F": " '/Bank[ \t]*Balance/&& $2!~/\$-/{print $2}' run*.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM