Replace x Number of String Occurrence with Sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace x Number of String Occurrence with Sed
# 1  
Old 01-10-2011
Replace x Number of String Occurrence with Sed

Ok,

So I have a huge file that has over 12000 lines in it.

in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file.

How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use four-minute-interval"?

this is as far as i got:

Code:
sed 's/use five-minute-interval/use five-minute-interval/g' services.cfg

# 2  
Old 01-10-2011
If you could find the first line number to change, like 250 in this example, then it is not hard:
Code:
sed '250,$s/use five-minute-interval/use four-minute-interval/g' services.cfg

Same line works in vi with ':', ex, ed. Usually, you find the first line in vi and say ':.,$s . . . .'
# 3  
Old 01-10-2011
Question here:

Code:
 
Does the search string occures only once in any line in your input file or might be occur at multiple times in the same line?
If not , how do you want to proceed

# 4  
Old 01-10-2011
Quote:
Originally Posted by panyam
Question here:

Code:
 
Does the search string occures only once in any line in your input file or might be occur at multiple times in the same line?
If not , how do you want to proceed


Yes, the search string occurs only once in each line. I'd like to do this from the command line.

if it'll be difficult to make the changes on the "last" 250 occurrence of the search string, then I can work with the first 250 occurrences of the search strings.

thanks.
# 5  
Old 01-10-2011
As suggested by DGPickett , you can use sed.

To get the line number of the 250th occurance:

Code:
 
line_num=`awk '/string to search/ {c++; if (c>=250) {c=NR;exit}}END {print c}' input_file`

then you can use $line_num value.

Code:
 
sed  "$line_num,$ s/string to search/string to replace/g" input_file

# 6  
Old 01-10-2011
Quote:
Originally Posted by panyam
As suggested by DGPickett , you can use sed.

To get the line number of the 250th occurance:

Code:
 
line_num=`awk '/string to search/ {c++; if (c>=250) {c=NR;exit}}END {print c}' input_file`

then you can use $line_num value.

Code:
 
sed  "$line_num,$ s/string to search/string to replace/g" input_file

This doesn't seem to be what i want.

ok, the file that needs to be edited is services.cfg.

if i did a grep -c "use five-minute-interval" on that file right now, i would get about 590 lines that have those strings.

but, i want to cut that 590 to half by replacing half of the "five-minute-interval" with "four-minute-interval".

there has to be a way for me to be able to automatically replace the first number of occurrences of a string with another string.

I tried the below but it didn't work. I also tried what you suggested, but it didn't replace the bottom 250 occurrences of the specified strings. Smilie

Code:
sed 's/use five-minute-interval/use four-minute-interval/250' services.cfg

# 7  
Old 01-10-2011
To replace the first 250 numbers of the string you can do something like:
Code:
awk 'c<250{c+=sub("use five-minute-interval","use four-minute-interval")}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace first occurrence of a string in while loop

####Solved#### Hello, My aim is to replace searched string with incremented value under ubuntu 16.04. Example: aasasasas 9030 31wwo weopwoep weerasas 9030 ew31wo ieopwoep bbqqqsas 9030 ew3swo ieeopwoep ccsaqpas 9030 ewiro o2opwoep Expected: aasasasas 9030 31wwo weopwoep weerasas 9031... (2 Replies)
Discussion started by: baris35
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

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Sed diffrent replace by occurrence

I couldn't find the answer anywhere, so I hope you could help me. I need to change something like the following: something/bla/aaaa anything/bbb to: something --bla ----aaaa anything --bbb How do I do this? Is it possible with sed? I tried various patterns, but don't know how to... (5 Replies)
Discussion started by: Patwan
5 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

7. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

8. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. UNIX for Dummies Questions & Answers

Search and replace to first occurrence of string

Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average... (7 Replies)
Discussion started by: gilmord
7 Replies
Login or Register to Ask a Question