Replace a specific word every three occurences
Dear all,
I got this problem today. I have a log file with several lines and columns. In a column I have for example this succession of strings: Picture word3 word3 word3 Picuture word3 Picture word3 word3 Picture word3 word3 Picture word3 What I need is a command that replace the string "word3" with let's say " bob" only every three occurences of the "word3" itself, as below: Picture BOB word3 word3 Picuture BOB Picture word3 word3 Picture BOB word3 Picture word3 I'm using this command that of course changes all of them, recoursively: awk '{for (i=1; i <= NF; i++) if ($i == "word3") $i = "BOB"; print $0}' file.log > file1.txt Any suggestions would be highly appreciated! All the best, Udiubu |
First solution:
(1) sub every three.. awk '/word3/{c++;if(c==3){sub("word3","BOB");c=0}}1' file.log >file.txt (2) sub every two.. awk '/word3/{c++;if(c==2){sub("word3","JACK");c=0}}1' file.txt >file1.txt |
Quote:
Code:
awk '/word3/{if(!c++){sub("word3","BOB");}if(c==3)c=0;}1' file.log > file1.txt |
Hi Cedrik,
That works, too!! Thanks.. I'm doing these three steps now as I found useful to have these three occurences named with three different names. Have a nice day. All the best, Udiubu |
Quote:
Code:
awk '/word3/ { if (++c == 3) { c=0; sub(/word3/, "BOB") } } 1' file.log > file1.txt |
Quote:
|
All times are GMT -5. The time now is 08:31 PM. |