osdir.com
mailing list archive

Subject: RE: Adding text after first occurrence only - msg#00070

List: editors.sed.user

Date: Prev Next Index Thread: Prev Next Index

Andre asked:

> I would like to add something to only the first ocurrence of </p>

This is in the sed FAQ, here:

# How do I match only the first occurrence of a pattern?

http://sed.sourceforge.net/sedfaq4.html#s4.11
<http://sed.sourceforge.net/sedfaq4.html#s4.11>

If you have GNU sed and a bash shell, the answer is this:

sed '0,\|</p>| s|</p>|&-your-extra-text-|' infile >outfile

--
Eric Pement






------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/dkFolB/TM
--------------------------------------------------------------------~->

--

Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> To unsubscribe from this group, send an email to:
sed-users-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/







Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

Re: Adding text after first occurrence only

Toen wij Andre Marcanth kietelden, kwam er dit uit: > Ruud H.G. van Tol: >> An ugly one again: >> sed -e '1,1 s:</p>:&<line 1>:; t' -e '1,/<\/p>/ s::&<line 2+>:' >> (ugly, because the replacement-string is mentioned twice) > It worked!! Thank you very much! I think I understood some things, but > others I could not. What I didn't understand is: > > & See man sed: An ampersand (``&'') appearing in the replacement is replaced by the string matching the RE. > ; t See man sed again: [2addr]t [label] Branch [...] if any substitu- tions have been made since the most recent reading of an input line or execution of a ``t'' function. If no label is specified, branch to the end of the script. > &<line 2+> (How does this work?) <line 1> is what I want to add but > didn't understand the &<line 2+ part> It was meant as documentative. Just put whatever-replacement-you- need in both: sed -e '1,1 s:</p>:&<replacement>:; t' -e '1,/<\/p>/ s::&<replacement>:' > I see you used : instead of /. This way we don't need to escape > special characters, right? Yes. In the addresses-part it works differently. -- Grtz, Ruud ------------------------ Yahoo! Groups Sponsor --------------------~--> $9.95 domain names from Yahoo!. Register anything. http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

Next Message by Date: click to view message preview

Re: Adding text after first occurrence only

Toen wij Pement, Eric kietelden, kwam er dit uit: > Andre asked: >> I would like to add something to only the first ocurrence of </p> > [from the sed FAQ] > If you have GNU sed and a bash shell, the answer is this: > > sed '0,\|</p>| s|</p>|&-your-extra-text-|' infile >outfile [from the same sed FAQ] Without GNU sed: sed -e '/<\/p>/ {s//&-extra-/;:loop' -e '$!N;$!bloop' -e '}' (on the prompt you might need to escape the exclamation marks) -- Grtz, Ruud ------------------------ Yahoo! Groups Sponsor --------------------~--> $9.95 domain names from Yahoo!. Register anything. http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

Previous Message by Thread: click to view message preview

Re: Adding text after first occurrence only

Toen wij Andre Marcanth kietelden, kwam er dit uit: > Ruud H.G. van Tol: >> An ugly one again: >> sed -e '1,1 s:</p>:&<line 1>:; t' -e '1,/<\/p>/ s::&<line 2+>:' >> (ugly, because the replacement-string is mentioned twice) > It worked!! Thank you very much! I think I understood some things, but > others I could not. What I didn't understand is: > > & See man sed: An ampersand (``&'') appearing in the replacement is replaced by the string matching the RE. > ; t See man sed again: [2addr]t [label] Branch [...] if any substitu- tions have been made since the most recent reading of an input line or execution of a ``t'' function. If no label is specified, branch to the end of the script. > &<line 2+> (How does this work?) <line 1> is what I want to add but > didn't understand the &<line 2+ part> It was meant as documentative. Just put whatever-replacement-you- need in both: sed -e '1,1 s:</p>:&<replacement>:; t' -e '1,/<\/p>/ s::&<replacement>:' > I see you used : instead of /. This way we don't need to escape > special characters, right? Yes. In the addresses-part it works differently. -- Grtz, Ruud ------------------------ Yahoo! Groups Sponsor --------------------~--> $9.95 domain names from Yahoo!. Register anything. http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

Next Message by Thread: click to view message preview

Re: Adding text after first occurrence only

Toen wij Pement, Eric kietelden, kwam er dit uit: > Andre asked: >> I would like to add something to only the first ocurrence of </p> > [from the sed FAQ] > If you have GNU sed and a bash shell, the answer is this: > > sed '0,\|</p>| s|</p>|&-your-extra-text-|' infile >outfile [from the same sed FAQ] Without GNU sed: sed -e '/<\/p>/ {s//&-extra-/;:loop' -e '$!N;$!bloop' -e '}' (on the prompt you might need to escape the exclamation marks) -- Grtz, Ruud ------------------------ Yahoo! Groups Sponsor --------------------~--> $9.95 domain names from Yahoo!. Register anything. http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by