Third line occurrence replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Third line occurrence replacement
# 1  
Old 07-03-2008
Third line occurrence replacement

You guys are really smart, so I'm hoping someone can help me out with this. I would like to match the third line occurrence of a pattern at the beginning of a line in a file and replace it. I'm ok at using sed but I have no clue about this one. Thanks in advance.
# 2  
Old 07-03-2008
What have you tried so far?
# 3  
Old 07-03-2008
Code:
sed "s/^pat1/pat2/" file

But I know it will do it for every occurrence. I've looked through some of the sed options but I can't figure out how to use them for what I'm trying to do.

Last edited by sirokket16; 07-03-2008 at 12:26 PM..
# 4  
Old 07-03-2008
Please provide data sample.
# 5  
Old 07-03-2008
Maybe I should be more clear with the issue I'm having. I want to find the third occurrence at the beginning of a line. So if I have a file that reads:

blah blah
blah
pat1 blah
pat1
pat1 blah // <- find this one and replace with pat2
pat1
.
.
blah

I want to find the third pat1 and overlook the first two.
# 6  
Old 07-03-2008
something like:

Code:
#  awk '{CNT[$1]+=1; if ( $1=="pat1" && CNT[$1]==3 ){print "pat2"} else {print $0}}' blah
blah blah
blah
pat1 blah
pat1
pat2
pat1

should work

Last edited by Tytalus; 07-04-2008 at 07:45 AM.. Reason: tweaked for accuracy - was replacing more than it should
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

3. Shell Programming and Scripting

Cutting commas after the second occurrence in a line

Hello everyone, I am manipulating a large CSV file and am trying to read it into a program and started running into trouble. The have manually edited the file trying to make it correctly run through the program and have made progress. However, I am know stuck with an issue involving too many... (3 Replies)
Discussion started by: tastybrownies
3 Replies

4. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

5. Shell Programming and Scripting

Count number of occurrence at each line

Hi I have the following file ENST001 ENST002 4 4 4 88 9 9 ENST004 3 3 3 99 8 8 ENST009 ENST010 ENST006 8 8 8 77 8 8 Basically I want to count how many times ENST* is repeated in each line so the expected results is 2 1 3 Any suggestion please ? (4 Replies)
Discussion started by: fuad_
4 Replies

6. Shell Programming and Scripting

Merging several line in one based on an occurrence

Hi, I try to gather several line in one based on the first column. For exemple : toto;1;2;3 toto;4;5;6 toto;7;8;9 etc... (Number of lines not predefined) ruru;a;b;c ruru;d;e;f ruru;g;h;i ... I'm trying to get : toto;1;2;3;4;5;6;7;8;9... ruru;a;b;c;d;e;f;g;h;i...Thank you. (2 Replies)
Discussion started by: xanthos
2 Replies

7. UNIX for Dummies Questions & Answers

Occurrence of two strings in a line of a file

Hi all, is there a way to find the occurrence of two strings in a line of a file? e.g. I have XXXX yyyyy zzzz 111 XXXX yyyyy zzzz 222 XXXX yyyyy zzzz 333 XXXX yyyyy zzzz 444 and want to find in one shot XXXX yyyyy zzzz 222 Thank you,... (9 Replies)
Discussion started by: f_o_555
9 Replies

8. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question