需要为自己和邻近的通道打印匹配正则表达式的行

时间:2016-07-14 23:55:31

标签: awk

我有问题因此我问:) 我有输入......这样的事情

48 06-Jul-16 00:04:26.850000, 0.3
  1 06-Jul-16 00:04:29.200000, 0.35
 60 06-Jul-16 00:04:29.250000, 0.3
  1 06-Jul-16 00:04:32.190476, 0.35
 11 06-Jul-16 00:04:32.238095, 0.3
  1 06-Jul-16 00:04:32.761905, 0.35
 20 06-Jul-16 00:04:32.809524, 0.3
  1 06-Jul-16 00:04:33.800000, 0.35
 14 06-Jul-16 00:04:33.850000, 0.3
  1 06-Jul-16 00:04:34.550000, 0.35
  4 06-Jul-16 00:04:34.600000, 0.3
  1 06-Jul-16 00:04:34.800000, 0.35
 28 06-Jul-16 00:04:34.850000, 0.3
  2 06-Jul-16 00:04:36.238095, 0.35
 12 06-Jul-16 00:04:36.333333, 0.3
  1 06-Jul-16 00:04:36.904762, 0.35
  1 06-Jul-16 00:04:36.952381, 0.3
  1 06-Jul-16 00:04:37.000000, 0.35
 22 06-Jul-16 00:04:37.050000, 0.3
  2 06-Jul-16 00:04:38.150000, 0.35
 10 06-Jul-16 00:04:38.250000, 0.3
  1 06-Jul-16 00:04:38.750000, 0.35
  1 06-Jul-16 00:04:38.800000, 0.3

我需要一个输出,其中第一列是1,它是前一行,下一行第1列必须大于12和第四列都大于或低于当前行第4列,如下

  1 06-Jul-16 00:04:29.200000, 0.35
  1 06-Jul-16 00:04:33.800000, 0.35

我试着用awk玩一下但没有白费

awk '($1=1) && NR+1($1>12) && NR-1($1>12){print $0}'

我明白这是完全错误的。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

$ cat tst.awk
NR==FNR { a[NR] = $0; next }
{ split(a[FNR-1],p); split(a[FNR+1],n) }
(FNR > 1) && ($1 == 1) && (p[1] > 12) && (n[1] > 12) &&
    ( ( (p[4] > $4) && (n[4] > $4) ) ||
      ( (p[4] < $4) && (n[4] < $4) ) )

$ awk -f tst.awk file file
  1 06-Jul-16 00:04:29.200000, 0.35
  1 06-Jul-16 00:04:33.800000, 0.35

或者如果您更喜欢一次通过,而不是更复杂:

$ cat tst.awk
{ split(prev,p); split(curr,c); split($0,n) }
(NR > 2) && (c[1] == 1) && (p[1] > 12) && (n[1] > 12) &&
    ( ( (p[4] > c[4]) && (n[4] > c[4]) ) ||
      ( (p[4] < c[4]) && (n[4] < c[4]) ) ) {
        print curr
}
{prev = curr; curr = $0 }

$ awk -f tst.awk file
  1 06-Jul-16 00:04:29.200000, 0.35
  1 06-Jul-16 00:04:33.800000, 0.35

p = previous,c = current,n = next。

答案 1 :(得分:0)

$ cat > test.awk
t[1]==1 && p[1]>12 && $1>12 && ((t[4]>p[4] && t[4]>$4) || (t[4]<p[4] && t[4]<$4)) {
  print t[0]
} 
{
  p[1]=t[1];t[1]=$1;
  p[0]=t[0];t[0]=$0;
  p[4]=t[4];t[4]=$4;
}


$ awk -f test.awk test.in
  1 06-Jul-16 00:04:29.200000, 0.35
  1 06-Jul-16 00:04:33.800000, 0.35

答案 2 :(得分:0)

这只会读取一次文件:

$1 == 1 {
    line[1] = $0;
    value[1] = $NF;
    next;
}
$1 > 12 {
    if (line[1] && line[0]) {
        if ((value[0] > value[1] && $NF > value[1]) || (value[0] < value[1] && $NF < value[1])) {
            print line[1];
            line[1] = "";
        }
    }
    line[0] = $0;
    value[0] = $NF;
    next;
}
{
    line[0] = "";
}