LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 08-25-2009, 12:42 PM   #1
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Rep: Reputation: 15
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
 
Old 08-26-2009, 05:10 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
Old 08-26-2009, 01:11 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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
 
Old 08-26-2009, 01:34 PM   #4
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks for all the input! I will keep it saved and try it when I get a chance! you guys rock!

-Rod
 
Old 08-26-2009, 03:54 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 08-26-2009, 07:29 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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.
 
Old 08-26-2009, 08:08 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
touche!!!
 
Old 08-27-2009, 09:59 AM   #8
bridrod
Member
 
Registered: Aug 2009
Distribution: SLES, openSUSE
Posts: 39

Original Poster
Rep: Reputation: 15
Question

Quote:
Originally Posted by pixellany View Post
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
 
  


Reply

Tags
lines, range, replace, replacement, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED and Replacing Specific Line bridrod Linux - Newbie 6 08-24-2009 12:28 PM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM
replacing specific lines in a text document stellarmarine1 Linux - General 1 09-07-2004 02:34 PM


All times are GMT -5. The time now is 06:15 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration