我想将一行文本移动到下面一行的末尾,并复制 x 行移动的值,直到找到该模式的新匹配项。我已经弄清楚了文件的所有其他文本处理,但是模式之间的复制和粘贴让我感到困惑。
这可以通过 AWK/sed/grep 实现吗?我无法使用 csplit,因为实际文件非常大,因此会很快填满磁盘。
谢谢!
举个例子:我有以下文本文件:
Voting Round 0
Ag_1 || 1 || 0 || 1 |
Ag_2 || 1 || 1 || 0 |
Ag_2 || 1 || 1 || 0 |
Ag_11 || 1 || 1 || 0 |
Voting Round 1
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Voting Round 2
Ag_1 || 1 || 0 || 1 |
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Voting Round 3
Ag_1 || 1 || 0 || 1 |
Ag_11 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
Ag_1 || 1 || 1 || 0 |
我想生成这样的东西:
Ag_1 || 1 || 0 || 1 | 0
Ag_2 || 1 || 1 || 0 | 0
Ag_2 || 1 || 1 || 0 | 0
Ag_11 || 1 || 1 || 0 | 0
Ag_1 || 1 || 1 || 0 | 1
Ag_1 || 1 || 1 || 0 | 1
Ag_1 || 1 || 1 || 0 | 1
Ag_1 || 1 || 1 || 0 | 1
Ag_1 || 1 || 0 || 1 | 2
Ag_1 || 1 || 1 || 0 | 2
Ag_1 || 1 || 1 || 0 | 2
Ag_1 || 1 || 1 || 0 | 2
Ag_1 || 1 || 0 || 1 | 3
Ag_11 || 1 || 1 || 0 | 3
Ag_1 || 1 || 1 || 0 | 3
Ag_1 || 1 || 1 || 0 | 3
答案1
这可能是您想要做的:
$ awk '/\|/{print $0 n; next} {n=$NF} !NF' file
Ag_1 || 1 || 0 || 1 |0
Ag_2 || 1 || 1 || 0 |0
Ag_2 || 1 || 1 || 0 |0
Ag_11 || 1 || 1 || 0 |0
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 0 || 1 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 0 || 1 |3
Ag_11 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
或者可能:
$ awk '/\|/{print $0 c+0; next} !NF{print; c++}' file
Ag_1 || 1 || 0 || 1 |0
Ag_2 || 1 || 1 || 0 |0
Ag_2 || 1 || 1 || 0 |0
Ag_11 || 1 || 1 || 0 |0
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 0 || 1 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 0 || 1 |3
Ag_11 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
或者:
$ awk '/Voting/{c=$NF; next} {print $0 (NF ? c : "")}' file
Ag_1 || 1 || 0 || 1 |0
Ag_2 || 1 || 1 || 0 |0
Ag_2 || 1 || 1 || 0 |0
Ag_11 || 1 || 1 || 0 |0
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 0 || 1 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 0 || 1 |3
Ag_11 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
有很多可能性。
答案2
gawk/mawk/nawk '/^V/{__=substr($NF,(getline)^_)\
\
} !NF || ($+_=($+_)__)'
Ag_1 || 1 || 0 || 1 |0
Ag_2 || 1 || 1 || 0 |0
Ag_2 || 1 || 1 || 0 |0
Ag_11 || 1 || 1 || 0 |0
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 1 || 0 |1
Ag_1 || 1 || 0 || 1 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 1 || 0 |2
Ag_1 || 1 || 0 || 1 |3
Ag_11 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3
Ag_1 || 1 || 1 || 0 |3