开发者

Why isn't my perl one liner working?

I'm trying to grab the number of errors from a HTML log file. Here's my code (yes, I know I'm mixing bash and perl):

 for line in `cat paths.txt`; do   grep Failed ${li开发者_如何转开发ne} | perl -pe 'm/\d+/'; done

I'm expecting to simply get a list of numbers, e.g.,

0 
1 
0 
0 
0 
1 
6

But instead I'm getting the entire line that contains the error, e.g.,

      Total Failed: <strong>0</strong>
      Total Failed: <strong>1</strong>
      Total Failed: <strong>0</strong>
      Total Failed: <strong>0</strong>
      Total Failed: <strong>0</strong>
      Total Failed: <strong>1</strong>
      Total Failed: <strong>6</strong>

What am I over looking?


perl -pe 'm/\d+/'; Will simply perform a pattern match, the line printed will not be changed. If you want to only print the number, you need something like:

perl -pe 's/\D+//g'          # change $_

or

perl -nE 'say $1 if /(\d+)/' # only print the match

But why not use all perl?

perl -nwE 'say $1 if /Failed.*(\d+)/' paths.txt


Try using this instead:

perl -ne '/\d+/ && print "$&\n"'


Why not simply this?

grep -o '[0-9]\+' paths.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜