awk脚本需要修正或可能grep的解决方案(新AWK) [英] awk script needs revision or possibly grep solution (new to awk)

查看:117
本文介绍了awk脚本需要修正或可能grep的解决方案(新AWK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集就像我在这里联系: http://pastebin.com/7tpBAqua

请注意前两行不是数据(数字),尽管如此,2号线与3号线有关。同样,第4行与第5行相关联,依此类推。

目前,我们有一个awk脚本,对那名阈值(低于-1以上1东西)首先行号输出的信息,这是输出:

  71
72
88
98
99
.... 等等...

如果数字是偶数,我们需要输出,后奇数号码(即如果72,然后再输出72个新行,然后73)

如果数为奇数那么我们需要输出,甚至之前(即,如果99后输出98个新行,然后99)。

  70
71
72
73
88
89

等等......

再次的想法是,我们在寻找噪声这个数据集,因此,我们需要消除它的研究是有效的。感谢您的帮助,您可以提供。

编辑:下面提供的解决方案,我决定把它分解为自己的个人学习以及为其他人谁可以读取这样的:

 的awk -F'[,]''NR大于2 {为(i = 2; I< = NF;我+ +)如果($ I< -1 || $ I&GT 1)打印(NR%2 == 0)NR ORS NR + 1:NR  -  1 ORS NR;接下来}'file.txt的

首先,我们将制作一个基本的算法:

 如果(当前==偶数)
  打印CUR + \\ N + preV
否则,如果(CUR ==奇)
  打印preV + \\ N + CUR-F'[,]'#现场分隔符的标志和指定它[,]'NR>迄今为止看到2#输入记录的总数。{用于(ⅰ= 2; I&下; = NF;我++)#为循环开始于2,结束时大于或等于NR如果($ I< -1 || $ I> 1)当这些条件得到满足,然后#打印(NR%2 == 0)#打印NR模量2?NR ORS NR + 1#当前或者下:NR - 1 ORS NR; #的比较?接下来}'#现​​在去下一个NRfile.txt的#保存到FILE.TXT


解决方案

下面是一个使用 GNU AWK 一种方式,有些人是previous code

 的awk -F'[,]''NR大于2 {为(i = 2; I< = NF;我+ +)如果($ I< -1 || $ I> 1)印刷(NR%2 == 0)? NR ORS NR + 1:NR  -  1 ORS NR;接下来}'file.txt的

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

解决方案

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

这篇关于awk脚本需要修正或可能grep的解决方案(新AWK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆