Text-Processing

將特定值附加到模式之間的行尾

  • March 17, 2022

我想將一行文本移動到下面一行的末尾,並複制為 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

這可能是你想要做的:

$ 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

有很多可能性。

引用自:https://unix.stackexchange.com/questions/694504