Yoda
Yoda

Reputation: 3

How can I change a character in line every n lines using awk?

Let's say I have this kind of file:

         248
 STEP:           1
 C      0.975156      8.208886     -0.860474
 C      1.519935      8.610952      0.374438
 C      1.771419      9.990898      0.592491
 C      1.418414     10.881772     -0.466490

         248
 STEP:           6
 C      0.985225      8.205446     -0.859378
 C      1.520091      8.611807      0.372435
 C      1.775156      9.991095      0.588109
 C      1.415878     10.886006     -0.465814

with ~1500000 lines

and I need to have sth like this:

         248
 STEP:           1
 C      0.975156      8.208886     -0.860474
 C      1.519935      8.610952      0.374438
 D      1.771419      9.990898      0.592491
 C      1.418414     10.881772     -0.466490

         248
 STEP:           6
 C      0.985225      8.205446     -0.859378
 C      1.520091      8.611807      0.372435
 D      1.775156      9.991095      0.588109
 C      1.415878     10.886006     -0.465814

how can I achive this with awk/sed/whatever?

Upvotes: 0

Views: 53

Answers (1)

karakfa
karakfa

Reputation: 67567

awk to the rescue!

$ awk '/^ STEP/{c=0}  /^ C/ && ++c==3{sub(/C/,"D")}1' file

or with count-down

$ awk '/^ STEP/{c=3} /^ C/ && !--c{sub(/C/,"D")}1' file

if your file doesn't have leading spaces remove them from the patterns as well. In the posted file above you have them.

Upvotes: 1

Related Questions