LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-22-2010, 02:14 AM   #1
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Rep: Reputation: 15
Thumbs down Modify the only one pattern among two patterns in a text file


Hi
I want to replace a pattern with other pattern in a textfile.

But there are two same patterns,but I need two change only the second occurence.

EG:

Text file is
aaaa=1
bbbb=2
cccc=3
dddd=4
aaaa=x
gggg=6
ffff=7

Now I want to change aaaa=x into some other entry.

Thanks for your help.
 
Old 11-22-2010, 02:28 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

If you know what x is you can include that in the pattern: sed 's/aaaa=x/aaaa=Y/' file
If x is not known but you do know it is not a number (first entry has a number): sed 's/aaaa=[a-z]/aaaa=Y/' file
If you know that the second pattern is after a certain line number: sed '4,$s/aaaa=.*/aaaa=Y/'

You need to look for a uniqueness (examples 1 and 2) or a safe range (example 3).

Hope this helps.
 
Old 11-22-2010, 02:32 AM   #3
linuxromeo
Member
 
Registered: Jun 2010
Posts: 45

Original Poster
Rep: Reputation: 15
Hi

Iam sorry.Actually the second pattern is also aaaa=1.
Text file is
aaaa=1
bbbb=2
cccc=3
dddd=4
aaaa=1
gggg=6
ffff=7

Line number may vary.Only thing sure is to change the second occurence.
 
Old 11-22-2010, 03:43 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

Here's an awk solution:

awk '/aaaa=1/{n+=1}{if (n==2){sub("aaaa=1","aaaa=Y",$0)};print }' infile

Example run:
Code:
$ cat infile
aaaa=1
bbbb=2
cccc=3
dddd=4
aaaa=1
gggg=6
ffff=7

$ awk '/aaaa=1/{n+=1}{if (n==2){sub("aaaa=1","aaaa=Y",$0)};print }' infile
aaaa=1
bbbb=2
cccc=3
dddd=4
aaaa=Y
gggg=6
ffff=7
Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] BASH: how to substitute just one occurency of a pattern in a text file carolflb Linux - Newbie 8 11-19-2009 03:31 AM
Pattern matching in a text file - use of AWK?? wtaicken Programming 19 02-06-2009 05:54 PM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM
search / count unique patterns in text file logicalfuzz Linux - Newbie 2 10-14-2006 07:58 AM
how to modify the file naming pattern (%s~) of backups LinToWinWin Linux - Software 1 10-11-2005 08:27 PM


All times are GMT -5. The time now is 10:27 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration