awk script needs revision or possibly grep solution (new to awk)

為{幸葍}努か 提交于 2019-12-12 17:19:40

问题


I have a dataset like I have linked here: http://pastebin.com/7tpBAqua

Note the first two lines are not data(numbers), despite this, the 2nd line is associated with the 3rd line. Similarly, the 4th line is associated with the 5th line, and so on.

Currently, we have an awk script that outputs info on all line numbers that were above the threshold value (anything below -1 and above 1), this is the output:

71
72
88
98
99
.... and so on...

If the number is even we need to output the number that is odd after it (ie if 72, then output 72 new line then 73)

If the number is odd then we need to output the even before it (ie if 99 then output 98 new line then 99).

70
71
72
73
88
89

And so on…

Again, the idea is we are finding noise in this data-set and thus we need to eliminate it for the research to be valid. Thanks for any help you can provide.

Edit: from the solution provided below I have decided to break it down for my own personal learning as well as for anyone else who may read this:

"awk -F'[ ,]' 'NR>2{for (i=2;i<=NF;i++) if ($i<-1 || $i>1) print (NR%2==0) ? NR ORS NR + 1 : NR - 1 ORS NR; next }' file.txt

First we will craft a basic algorithm:

if (cur == even)
  print cur + \n + prev 
else if (cur == odd)
  print prev + \n + cur

-F'[ ,]'   # a flag for field seperator and designating it with [ ,]

'NR>2      # The total Number of input Records seen so far. 

{for (i=2;i<=NF;i++) # for loop starting at 2, ending when greater or equal to NR

if ($i<-1 || $i>1) # when these conditions are met then 

print (NR%2==0)    # print NR modulus 2

? 

NR ORS NR + 1      # current OR next

: NR - 1 ORS NR;   # comparisons?

next }'            # now go to the next NR

file.txt           # save to file.txt

回答1:


Here's one way using GNU awk and some of you're previous code:

awk -F'[ ,]' 'NR>2{for (i=2;i<=NF;i++) if ($i<-1 || $i>1) print (NR%2==0) ? NR ORS NR + 1 : NR - 1 ORS NR; next }' file.txt



回答2:


Take the original code from this question, and put in a conditional that tests the parity of NR. (Parity is the property of being either even or odd, it is tested by using the modulus operator: %).

awk -F'[ ,]' 'NR>2{for (i=2;i<=NF;i++) if ($i>=-1 || $i<=1) { if(NR%2 == 0) { print NR+1 } else { print NR-1} ; next } }' 


来源:https://stackoverflow.com/questions/13131245/awk-script-needs-revision-or-possibly-grep-solution-new-to-awk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!