Replace first 5 occurance of a pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace first 5 occurance of a pattern
# 1  
Old 10-17-2008
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.
# 2  
Old 10-17-2008
Code:
perl -i.bck -pe's/(abc)/$c++<5?xyz:$1/eg' a.txt

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

2. 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

3. 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

4. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

Need 10 lines before the first occurance of a pattern

Here is the text file: 1This is a text file 2this is a text file 3This is a text file 4this is a text file 5This is a text file 6this is a text file 7This is a text file 8this is a text file 9This is a text file 10this is a text file 11This is a text file 12this is a text file 13This... (4 Replies)
Discussion started by: Johny001
4 Replies

6. Shell Programming and Scripting

Pattern matching first occurance only wanted

I have the following code: declare -a search search=(name label elapsed totalfilecount totalfilebytes starttime exitcode errors warnings fatals) for (( i = 0 ; i < ${#search} ; i++ )) do data=`awk -F '^'${search}'=\"' 'BEGIN{RS="\" "; OFS="\n"; ORS=""} { print $2 }' summary.alg` ... (1 Reply)
Discussion started by: Ikon
1 Replies

7. Shell Programming and Scripting

How to find files which has more than one occurance of pattern

Hello Everyone, Please help me in finding out the solution. The problem is .. lets say i have 600 files in a directory. All 600 files are shell script files. Now i need to find out the files which contains a pattern "SHELL" more than once. No matter how the pattern occurs , it can be in... (10 Replies)
Discussion started by: Prahlad
10 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. 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

10. UNIX for Dummies Questions & Answers

Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz". I used the following code: grep -ic "xyz" abc.txt but it is... (3 Replies)
Discussion started by: palash2k
3 Replies
Login or Register to Ask a Question