Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-25-2009, 12:42 PM
|
#1
|
|
Member
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39
Rep:
|
SED and Replacing Specific occurrence or Range of Lines
Created a new post because the question is different....
How would I replace entries from a file if I did not know the specific line, but knew it's the second occurrence in the file?
And how would I do it also if I only knew it's actually between a range of lines (i.e.: 155-190 lines)?
Any input is welcome!
Thanks,
-Rod
|
|
|
|
08-26-2009, 05:10 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,893
|
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):
Code:
sed '3,6 s/pattern/replacement/' file
|
|
|
|
08-26-2009, 01:11 PM
|
#3
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 323
Rep: 
|
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
|
|
|
|
08-26-2009, 01:34 PM
|
#4
|
|
Member
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39
Original Poster
Rep:
|
Thanks for all the input! I will keep it saved and try it when I get a chance! you guys rock!
-Rod
|
|
|
|
08-26-2009, 03:54 PM
|
#5
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
Code:
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.>>
Last edited by pixellany; 08-26-2009 at 04:01 PM.
|
|
|
|
08-26-2009, 07:29 PM
|
#6
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 323
Rep: 
|
pixellany, your solution doesn't cover the special case of the first and second 'OLD' both being on the same line; it needs two extra commands:-
Code:
sed '/OLD/{s/OLD/NEW/2;t2;:1 n;/OLD/{s/OLD/NEW/;:2 n;$!b2};b1}'
Your method does have the advantage of loading only one line at a time into the pattern space, which is useful for dealing with large files.
|
|
|
|
08-26-2009, 08:08 PM
|
#7
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
touche!!!
|
|
|
|
08-27-2009, 09:59 AM
|
#8
|
|
Member
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39
Original Poster
Rep:
|
Quote:
Originally Posted by pixellany
Code:
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!
TIA,
-Rod
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:03 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|