0

I would like to move a line of text to the end of the line below, and replicate the value moved for x lines, until a new match for the pattern is found. All the other text processing for my files I have figured out, but this copy and paste between patterns has me stumped.

Is this achievable with AWK/sed/grep? I cannot use csplit because the actual files are quite large, and so would fill up the disk very fast.

Thanks!

For an example: I have the following text file:

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      |

and I would like to generate something like this:

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
0

2 Answers 2

1

This may be what you're trying to do:

$ 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

or maybe:

$ 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

or:

$ 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

There are lots of possibilities.

1
  • 1
    Thank you! The third solution worked like a charm!
    – olipinski
    Commented Mar 16, 2022 at 12:32
0
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .