awk to replace second occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to replace second occurance
# 1  
Old 03-04-2010
awk to replace second occurance

Code:
#original file
.
.
~
~
Index=2  
xxx
replace  #dont replace 1st occurance
yyy
Index=2
xxx
replace  #substitue replace with "REPLACE"
yyy
Index=2
xxx
replace #substitue replace with "REPLACE"
yyy
Index=3
xxx
replace 
yyy

Code:
Expected output file:

.
.
~
~
Index=2
xxx
replace  
yyy
Index=2
xxx
REPLACE  
yyy
Index=2
xxx
REPLACE  
yyy
Index=3
xxx
replace 
yyy


Hi,

This is the simiplified file I am working on.. I need to :
1. search for 2nd occurance of a string pattern ("Index=2"in file)
2. Go forward 2 lines after this
3. Replace current line with new pattern (ex: "REPLACE" )



I am using awk for checking the 2nd oocurance, but dont know
how to step 2 lines forward and replace the string.

Code:
#awk.sh

/Index=2/{
   mcount++
   if(mcount == 2){
      {print NR+2}
      #need to substitute NR+2
   }
}

# 2  
Old 03-04-2010
Code:
awk '/^replace/ {c++}; (c==2)||(c==3) {$1=toupper($1)}1' urfile

# 3  
Old 03-04-2010
Try this:

Code:
awk '/Index=2/{print;getline;print;getline;if (cnt == 0)cnt+=1; else { gsub("replace","REPLACE")}}1' filename


cheers,
Devaraj Takhellambam
# 4  
Old 03-04-2010
Thanks guys...both the solutions solves my problem..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string until 3Rd occurance of forward slash(/)

I have a file abc.txt which has records like 456 /home/fgg/abdc.txt 3567 /home/fdss/vfgb.txt 23 /home/asd/dfght.txt I WANT TO REMOVE STRING UNTIL 3RD OCCURANCE OF FORWARD SLASH Output should be like abdc.txt vfgb.txt dfght.txt (5 Replies)
Discussion started by: himanshupant
5 Replies

2. Shell Programming and Scripting

Help search and replace the last occurance of match in a file

Hi I want to replace only the last occurance of "union all" in input file with ";" I tried with sed 's/union all/;/g' in my input file, it replaced in all lines of input file Eg: select column1,column2 from test1 group by 2 union all select column1,column2 from test2 group by 2 union all ... (9 Replies)
Discussion started by: antosr7
9 Replies

3. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

4. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

5. Shell Programming and Scripting

replace/delete odd occurance

how to delete/replace the the odd occurance of a pattern say newline character "\n" a,b,c,d e,f g, h, i, j, k, l m,n 1, 2, 3, 4, 5, 6 1, 3, 4, 5, 6, 7 the output should be a,b,c,de,f g, h, i, j, k, lm,n 1, 2, 3, 4,5, 6 1, 3, 4, 5, 6, 7 (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

6. Shell Programming and Scripting

Search and replace only the first occurance

Hi, I need to search a string and replace with nothing, but only the First occurring string using sed/perl/awk (3 Replies)
Discussion started by: suraj.sheikh
3 Replies

7. Shell Programming and Scripting

search and replace the last occurance of a match in a file

HI please let me know if there is any command to search and replace only the last occurence of a string in aline. Eg: " This cat is a cat with a tail longer of all cat." I need to replace only the last "cat" in the above line. thanks (3 Replies)
Discussion started by: harikris614
3 Replies

8. UNIX for Dummies Questions & Answers

Replace first 5 occurance of a pattern

There is scenario that i have to replace a pattern at the first 5 occurance in a file. say i need to replace 'abc' by 'xyz' at its first 5 occurance in a file a.txt, can anybody help me how it can be done, i can do complete replacement of the pattern using sed throughtout the file. (1 Reply)
Discussion started by: palash2k
1 Replies

9. Shell Programming and Scripting

How to replace specific text line out of multiple occurance

Hi I would like to replace specific line eg ExitAction = NONE to ExitAction = FALSE under only TASK sipsiproc and other ExitAction = NONE will remain as usual in the file(shell script) The file contains: TASK rgcdproc { CommandLine = $SSHOME/bin/rgcd.exe NewConsole... (5 Replies)
Discussion started by: madhusmita
5 Replies

10. UNIX for Dummies Questions & Answers

replace the n'th occurance in a file

Hi All, How can i replace the n'th occurance in a file. ? I have a property file like EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;1;valeur EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;2;valeur2... (2 Replies)
Discussion started by: subin_bala
2 Replies
Login or Register to Ask a Question