[SOLVED] SED and Replacing Specific occurrence or Range of Lines
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
1. I don't know a reliable method to replace the second occurrence using sed. In sed you can add a numeric flag to the substitution command to replace the second occurrence of a pattern on the same line, but if the second occurrence is on another line I'd suggest to use awk instead:
Code:
awk '/pattern/{
for (i = 1; i <= NF; i++){
if ($i == "pattern")
count++
if (count == 2)
sub("pattern","replacement",$i)
}
}1' file
this works in both cases where the second occurrence is on the same line of the first one or on another line.
2. To specify a range of lines in sed (for example from the 3rd to the 6th):
This uses GNU sed to replace the second occurrence of a pattern in a file.
It first loads the entire file into the sed pattern space, so this method will fail if the file is too big to fit into the available memory.
Some non-GNU versions of sed have fixed limits on the size of the pattern space. http://sed.sourceforge.net/sedfaq6.html#s6.6
Code:
sed ':a N;$!ba; s/pattern/replacement/2' infile > outfile
sed '/OLD/{:1 n;/OLD/{s/OLD/NEW/;:2 n;$!b2};b1}' filename > newfilename
I KNEW SED could do it!! Thanks to Kenhelm for this bit:
:2 n;$!b2
In my example, it's used to run thru the rest of the file without changing anything
What it does:
Looks for a line with "OLD"
....When found, it loops (#1)--getting the next line--till it finds another instance of "OLD"
........If found, it performs the substitution, then enters the "do nothing" loop (#2) to end of file
........If not, it falls thru to the end
....If not, it falls thru to the end
<<Edit: PS: The only advantage over Kenhelm's method is that it is maybe not so dependent on file size.>>
sed '/OLD/{:1 n;/OLD/{s/OLD/NEW/;:2 n;$!b2};b1}' filename > newfilename
I KNEW SED could do it!! Thanks to Kenhelm for this bit:
:2 n;$!b2
In my example, it's used to run thru the rest of the file without changing anything
What it does:
Looks for a line with "OLD"
....When found, it loops (#1)--getting the next line--till it finds another instance of "OLD"
........If found, it performs the substitution, then enters the "do nothing" loop (#2) to end of file
........If not, it falls thru to the end
....If not, it falls thru to the end
<<Edit: PS: The only advantage over Kenhelm's method is that it is maybe not so dependent on file size.>>
You guys rock! I tested all the scripts provided and they all worked great!
I just have difficulties understanding the different options from this particular script line:
sed '/OLD/{s/OLD/NEW/2;t2;:1 n;/OLD/{s/OLD/NEW/;:2 n;$!b2};b1}'
The explanation for each "session" provided by pixellany I understood fine, but WHY they are structure the way shown above is confusing.
Would one of you mind explaining each option from the SED line so a newbie like me can understand it? I tried reading the manual but holy! Is it complicated!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.