0

I have a text file with following lines for example.

Input

DD0TRANSID000019021210504250003379433005533665506656000008587201902070168304000.0AK  0000L00000.00  N          01683016832019021220190212N0000.001683065570067.000000.00000.0000000000000NAcknowledgment                                                                                                                                        
DD0TRANSID000019021210504250003379433005535567606656000008587201902085381804000.0FC  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NFirst Contact                                                                                                                                         
DD0TRANSID000019021210510360003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.0
CC0TRANSID000019021210510360003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.0

My requirement is If the line begins with "D" then replace string starting from 14th position to 27th Position with D00000X (X being a number with left padding)

If I come across the same value of string between 14th and 27th Position, that I encountered before in the file, I should replace with D00000X, else with D00000X+1

Output

DD0TRANSID00001902121D000006003379433005533665506656000008587201902070168304000.0AK  0000L00000.00  N          01683016832019021220190212N0000.001683065570067.000000.00000.0000000000000NAcknowledgment                                                                                                                                        
DD0TRANSID00001902121D000006003379433005535567606656000008587201902085381804000.0FC  0000L00000.00  N          53818538182019021220190212N0000.053818065570067.000000.00000.0000000000000NFirst Contact                                                                                                                                         
DD0TRANSID00001902121D000007003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.0
CC0TRANSID000019021210510360003379433005535568006656000008587201902085381804000.0SR  0000L00000.00  N          53818538182019021220190212N0000.0
  • 1
    Are you sure the replacement happens at position 14? The output doesn't seem right - I see the change at position 21 (or 22 if 1-based). – choroba Apr 8 at 14:16
  • Is'nt it the same question – ctac_ Apr 8 at 14:22
  • No, it isnt the same question. I need to repeat the replaced string, every time I encounter a duplicate string(string between 14-21 position) else keep incrementing. – Rohit Prasad Apr 8 at 14:40
0

Assuming that these are date/time stamps and increment in sequence then

a=($(grep -Po "(?<=^D.{13}).{13}" testfile | uniq))
for ((i=0; i<${#a[@]}; i+=1)); do
    s=${a[$i]}
    sed -E -i "s/(D.{13})$s/\1${s:0:7}D$(printf '%06d' $((i+1)))/g" testfile
done

If a stamp repeats further down the file it gets a new counter increment. If you don't want that then the uniq needs to be sort | uniq which will apply the same counter to the same stamp throughout the file.

BTW, looks like you removed the 0 at position 28 in your example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.