LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2011, 08:34 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Rep: Reputation: Disabled
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
 
Old 12-07-2011, 09:12 AM   #2
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-07-2011, 09:24 AM   #3
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by udiubu View Post
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
Nice, but why not do it in one pass, like:
Code:
awk '/word3/{if(!c++){sub("word3","BOB");}if(c==3)c=0;}1' file.log > file1.txt
 
1 members found this post helpful.
Old 12-07-2011, 09:41 AM   #4
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-07-2011, 10:38 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Cedrik View Post
Code:
awk '/word3/{if(!c++){sub("word3","BOB");}if(c==3)c=0;}1' file.log > file1.txt
You could make it even simpler:
Code:
awk '/word3/ { if (++c == 3) { c=0; sub(/word3/, "BOB") } } 1' file.log > file1.txt
 
Old 12-07-2011, 10:58 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Nominal Animal View Post
You could make it even simpler:
Code:
awk '/word3/ { if (++c == 3) { c=0; sub(/word3/, "BOB") } } 1' file.log > file1.txt
Simpler indeed, nice
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replace a whole string which contains a spefiic word(s) in it abubakar-malik Linux - Newbie 3 02-14-2011 07:46 AM
replace specific character after specific line by awk Syed Tarique Moin Programming 2 07-19-2010 01:47 PM
Global Replace A String That's Not A Whole Word onesikgypo Programming 5 09-18-2009 08:06 AM
replace word in a file ust Linux - Software 2 11-27-2007 08:39 PM
Replace word in VIM ICO Linux - Software 2 03-17-2004 01:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration