-1

I want to print all those line in which mp_demand>1 . I am using awk for this, but somehow not able to achieve that.

root@1.2.3.4:#cat a.txt
Target MR 11604 MPG: -1 Region: -1
reading assignments message /a/tmp/a.txt
mpg=1 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=1
mpg=2 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=1
mpg=3 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=1
mpg=4 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=1
mpg=5 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=1
mpg=6 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=34

What I am trying is

root@1.2.3.4:#  cat a.txt  | awk 'BEGIN {p=0}  $7  >= 0 {p ++} END {print p}' | head 
45877

However, the expected output I want is

mpg=6 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=34 

any other/better ways of doing it?

hitman99
  • 65
  • 7
  • 2
    I agree it is unfair to down vote a well drafted question like this. – anubhava Sep 21 '17 at 18:54
  • Maybe disgruntled Perl people for not seeing any Perl? Who knows. – Benjamin W. Sep 21 '17 at 18:57
  • Or maybe someone thought it's not useful to others. – ikegami Sep 21 '17 at 19:15
  • 1
    @hitman99, your criteria are unclear: did you mean `mp_demand>1` instead? – glenn jackman Sep 21 '17 at 19:22
  • @glennjackman , yes I meant mp_demand>1 instead. Ed, I fixed the input just now. It was actually truncated initially, as I just posted a part of the text. – hitman99 Sep 21 '17 at 21:20
  • I really do not understand, the need of a down vote here. I did my best to craft the question properly. I tagged perl because, I thought this can be solved by perl one liners as well, hence I asked "if there is a better way to solve this". – hitman99 Sep 21 '17 at 21:21
  • I'm guessing that by not accepting answers to any but one of your [previous questions](https://stackoverflow.com/users/5544515/hitman99) and not responding to some comments from some people trying to help you in those threads you may have ruffled some feathers. – Ed Morton Sep 21 '17 at 22:08
  • Hi Ed, Thank you for the clarification. I do not quite understand what you mean by "not accepting answers" but I presume it means , not upvoting them ? I just got voting power recently , and I upvote all the useful answers. But my apologies to anyone who might have been offended, this is a great learning community and did not want anyone to get offended. – hitman99 Sep 25 '17 at 15:47
  • @zdim thanks for tagging me, I didn't know hitman99 had responded. hitman99 - [you knew how to accept an answer last March](https://stackoverflow.com/a/35781963/1745001), I'm just talking about continuing to do the same for other questions. – Ed Morton Sep 27 '17 at 13:19

2 Answers2

3
$ awk -F'=' 'NR>2 && $NF>1' a.txt
mpg=6 mrule=11604 reg=33000 score=10625 rank=0 perc=100 mp_demand=34
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • thanks that helped :) Googling it tells me that NF is for the number of fields in the current record, but can you give a brief explanation as to how NF is used here. – hitman99 Sep 21 '17 at 21:41
  • 1
    With `-F'='` I've told awk the fields are `=`-separated so the last field, $NF, is the text after the last `=` on the line which is the value you want to test. – Ed Morton Sep 21 '17 at 21:54
2

With Perl (tagged), using the criterion of > 1 as the desired output indicates

perl -wnE'/mp_demand=([0-9]+)/ && $1 > 1 && print' a.txt

Or without the name, /=([0-9]+)$/,  if it's always last in the line (or you want the last number)

Note: It is generally not needed to cat and pipe the file into the command; most tools can feed the file line by line into their STDIN when you supply its name after the command (perhaps with <)

zdim
  • 64,580
  • 5
  • 52
  • 81