Find _next_ occurence with sed

This is a discussion on Find _next_ occurence with sed within the shell forums in Operating Systems category; On Tuesday 26 August 2008 19:33, pk wrote: > On Tuesday 26 August 2008 18:40, Oliver Witt wrote: > >> >>> Ok, so you may probably be able to do something like >>> >>> sed -n ',/searchpattern/{=;p;q;}' >> >> It's getting awfully close to what I need. I did not think of the q >> command. But I believe the addresses mean something else then I need. >> It seems to be returning all line numbers from line >> until the search pattern is matched, but due to the q , this always >> stops after the first line, thus always giving me . > > Make sure you are specifying ...

Go Back   Database Forum > Operating Systems > shell

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #11  
Old 08-26-2008, 02:38 PM
Default Re: Find _next_ occurence with sed

On Tuesday 26 August 2008 19:33, pk wrote:

> On Tuesday 26 August 2008 18:40, Oliver Witt wrote:
>
>>
>>> Ok, so you may probably be able to do something like
>>>
>>> sed -n ',/searchpattern/{=;p;q;}'

>>
>> It's getting awfully close to what I need. I did not think of the "q"
>> command. But I believe the addresses mean something else then I need.
>> It seems to be returning all line numbers from line
>> until the search pattern is matched, but due to the "q", this always
>> stops after the first line, thus always giving me .

>
> Make sure you are specifying the -n switch, and, for testing, try removing
> the "q". Which port of sed for windows are you using?


No, of course my command is wrong, sorry. Try this:

sed -n ',${/searchpattern/{=;p;q;}}'

(hoping your sed supports the nested construct)

Reply With Quote
  #12  
Old 08-26-2008, 02:41 PM
Default Re: Find _next_ occurence with sed

At 2008-08-26 12:40PM, "Oliver Witt" wrote:
>
> > Ok, so you may probably be able to do something like
> >
> > sed -n ',/searchpattern/{=;p;q;}'

>
> It's getting awfully close to what I need. I did not think of the "q"
> command. But I believe the addresses mean something else then I need.
> It seems to be returning all line numbers from line
> until the search pattern is matched, but due to the "q", this always
> stops after the first line, thus always giving me .


How about:

sed -n '0,d;/pattern/{=;q}' sed.file

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #13  
Old 08-26-2008, 02:42 PM
Default Re: Find _next_ occurence with sed

At 2008-08-26 12:40PM, "Oliver Witt" wrote:
>
> > Ok, so you may probably be able to do something like
> >
> > sed -n ',/searchpattern/{=;p;q;}'

>
> It's getting awfully close to what I need. I did not think of the "q"
> command. But I believe the addresses mean something else then I need.
> It seems to be returning all line numbers from line
> until the search pattern is matched, but due to the "q", this always
> stops after the first line, thus always giving me .


How about:

sed -n '0,d;/pattern/{=;q}' sed.file

I'll echo the sentiment that it's too bad you chose sed instead of awk.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Reply With Quote
  #14  
Old 08-26-2008, 02:47 PM
Default Re: Find _next_ occurence with sed

> No, of course my command is wrong, sorry. Try this:
>
> sed -n ',${/searchpattern/{=;p;q;}}'
>
> (hoping your sed supports the nested construct)


THANK YOU. That is exactly what I needed.
Reply With Quote
  #15  
Old 08-26-2008, 02:49 PM
Default Re: Find _next_ occurence with sed

> Only use sed for simple substitutions on a single line. For anything else use
> awk, perl, etc.:
>
> awk -v line=3 -v pattern="the" 'NR>=line && $0~pattern{print; exit}' file
>
> You may not need your C++ wrapper if you do it all in awk (or vice-versa).


Hm, I might do that next time, but for now sed seems to be doing what
I need. But you are right, it seems like it would be hard to extend my
current program for future purposes.

Oliver
Reply With Quote
  #16  
Old 08-26-2008, 02:50 PM
Default Re: Find _next_ occurence with sed

> How about:
>
> * * sed -n '0,d;/pattern/{=;q}' sed.file


That looks like a possibility as well, however, the file might contain
millions of lines. That is why I chose sed, because I thought it would
be able to do things the most efficient. And this does not look
particularly efficient, but I might be wrong about that.
Reply With Quote
  #17  
Old 08-26-2008, 02:51 PM
Default Re: Find _next_ occurence with sed

On Tuesday 26 August 2008 19:47, Oliver Witt wrote:

>> No, of course my command is wrong, sorry. Try this:
>>
>> sed -n ',${/searchpattern/{=;p;q;}}'
>>
>> (hoping your sed supports the nested construct)

>
> THANK YOU. That is exactly what I needed.


You're welcome. If you have GNU sed, Glenn Jackman's solution will work fine
too.

Reply With Quote
  #18  
Old 08-26-2008, 02:57 PM
Default Re: Find _next_ occurence with sed

On Tuesday 26 August 2008 19:50, Oliver Witt wrote:

>> How about:
>>
>> sed -n '0,d;/pattern/{=;q}' sed.file

>
> That looks like a possibility as well, however, the file might contain
> millions of lines. That is why I chose sed, because I thought it would
> be able to do things the most efficient. And this does not look
> particularly efficient, but I might be wrong about that.


Well, in all cases sed still has to read each time at least all the lines
from line 1 to the line that produces the first match after line
, so neither sed solution would be particularly efficient, I
guess.
But you could of course open the file from your C++ program, and read it
line by line and do pattern matching using C++ code (which would however be
an OT here).

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 09:16 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.