user5420138
user5420138

Reputation: 141

Replacing string by incrementing number

Input File

AAAAAA this is some content.
This is AAAAAA some more content BBBBBB. BBBBBB BBBBBB
This is yet AAAAAA some more BBBBBB BBBBBB BBBBBB content.

I can accomplish this partially with this code:

awk '/AAAAAA/{gsub("AAAAAA", "x"++i)}1' test.txt > test1.txt
awk '{for(x=1;x<=NF;x++)if($x~/BBBBBB/){sub(/BBBBBB/,"y"++i)}}1' test1.txt 

Output:

x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y4 y5 y6 content.

Anyway to get this output?

Expected Output:

x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.

Upvotes: 1

Views: 93

Answers (4)

anubhava
anubhava

Reputation: 786359

You may use this single awk:

awk '{
   j=0
   for (x=1; x<=NF; x++)
      if ($x ~ /^A{6}/)
         sub(/^A{6}/, "x" (++i), $x)
      else if ($x ~ /^B{6}/)
         sub(/^B{6}/, "y" (++j), $x)
} 1' file
x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.

Upvotes: 3

dawg
dawg

Reputation: 104111

Here is an alternate awk that is easily extended to add as many tags as you wish:

awk 'BEGIN{ rep["AAAAAA"]="x"; cnts["AAAAAA"]=1; reset["AAAAAA"]=0
            rep["BBBBBB"]="y"; cnts["BBBBBB"]=1; reset["BBBBBB"]=1
            # and so on...
        }
        {   
            for (e in rep) {
                cnts[e]=(reset[e]) ? reset[e] : cnts[e] 
                while ( sub(e,rep[e] (cnts[e]++) ) )
                    ; # empty statement since work is inside the while
        }
        } 1' file 

Prints:

x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.

Upvotes: 2

karakfa
karakfa

Reputation: 67567

another one

$ awk '{sub("AAAAAA","x"(++x)); y=0; while(sub("BBBBBB","y"(++y)));}1' file

x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.

Upvotes: 3

jfahne
jfahne

Reputation: 231

Just need to reset i=0 after each loop:

awk '/AAAAAA/{gsub("AAAAAA", "x"++i)}1' test.txt > test1.txt
awk '{for(x=1;x<=NF;x++)if($x~/BBBBBB/){sub(/BBBBBB/,"y"++i)}{i=0}}1' test1.txt 

Output:

x1 this is some content.
This is x2 some more content y1. y2 y3
This is yet x3 some more y1 y2 y3 content.

Upvotes: 2

Related Questions