テキスト行を下の行の末尾に移動し、新しいパターン一致が見つかるまで移動した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