0

I have the following sequence,

N
H
CB
CB
CG1
CG2
C
O
N
H
CB
CB
CG
CD
.
.
.

and I would like to replace the first "CB" with "CA" when "CB" is repeated in line below, so as to end up with the following sequence

N
H
CA
CB
CG1
CG2
C
O
N
H
CA
CB
CG
CD
.
.
.
2
  • If you were after the general case ("when the next line == the current line, decrement the current line") then be aware that the "what is predecessor of a string" is tricky. For example, what is the predecessor of "CG1"? Is is "CG0" or "CG" or something else? Commented Feb 28, 2019 at 17:50
  • sed -E '$!N;s/^(CB)(\n\1)$/CA\2/' Commented Mar 1, 2019 at 0:50

1 Answer 1

2

This sort of thing is a lot simpler to wrap your head around if you can read the file backwards. Fortunately, tac (which is cat backwards because that's what it does) is there to let us do this. We can then make a note when we see a "CB", and if we see "CB" on the next (formerly previous) line, edit it; then flip it back-to-front again with another use of tac:

$ tac input | awk '/CB/ && found==1 { $1="CA"; found=0 } /CB/ && found==0 { found=1 } ! /CB/ && found==1 { found=0 } {print}' | tac
N
H
CA
CB
CG1
CG2
C
O
N
H
CA
CB
CG
CD
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.