How to search and replace multiple number sequences in a file using bash

I have a file with a lot of lines, for example:

dog:7066469:182:0:0:7050964:7087402:7058396:7079290:7087537
cat:7066469:182:0:0:7050964:7087402:7058396
dog:7066469:182:0:0:7050964:7087402:7058396:7079290

      

Using bash programming (sed or awk or whatever) how can I add 6 before each number after the 5th ":", only lines starting with "cat:"? The correct result will be as follows:

dog:7066469:182:0:0:7050964:7087402:7058396:7079290:7087537
cat:7066469:182:0:0:67050964:67087402:67058396
dog:7066469:182:0:0:7050964:7087402:7058396:7079290

      

+3


source to share


1 answer


Using awk:



awk 'BEGIN{FS=OFS=":"} $1=="cat"{for (i=6; i<=NF; i++) $i = "6" $i} 1' file
dog:7066469:182:0:0:7050964:7087402:7058396:7079290:7087537
cat:7066469:182:0:0:67050964:67087402:67058396
dog:7066469:182:0:0:7050964:7087402:7058396:7079290

      

+5


source







All Articles