9

我有一个如下所示的输入文件(第一列是位置编号,第二列是应随时间增加的计数):

1       0
1       2
1       6
1       7
1       7
1       8
1       7
1       7
1       9
1       9
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10

我想修复它看起来像这样(替代计数与先前计数减少):

1       0
1       2
1       6
1       7
1       7
1       8
1       8
1       8
1       9
1       9
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10

我一直在尝试为此使用 awk ,但由于我似乎无法弄清楚如何重置行号(NR?)所以它会读取每一行,它是下一行,而不是两行一次。这是我到目前为止的代码,有什么想法吗?

awk '{a=$1; b=$2; getline; c=$1; d=$2; if (a==c && b<=d) print a"\t"b; else print c"\t"d}' original.txt > fixed.txt

此外,这是我目前得到的输出:

1       0
1       6
1       7
1       7
1       9
1       10
1       9
1       10
1       10
1       9
1       10
1       10
1       10
4

2 回答 2

8

也许你想要的只是:

awk '$2 < p { $2 = p } { p = $2 } 1' input-file

如果第二列中的值为负数,这将在第一行失败,因此请执行以下操作:

awk 'NR > 1 && $2 < p ...'

This simply sets the second column to the previous value if the current value is less, then stores the current value in the variable p, then prints the line.

Note that this also slightly modifies the spacing of the output on lines that change. If your input is tab-separated, you might want to do:

awk 'NR > 1 && $2 < p { $2 = p } { p = $2 } 1' OFS=\\t input-file
于 2012-07-28T21:43:27.350 回答
2

This script will do what you like:

{
  if ($2 < prev_count)
    $2 = prev_count
  else
    prev_count = $2

  printf("%d   %d\n", $1, $2)
}

This is a verbose version to be easily readable :)

于 2012-07-28T21:47:06.967 回答