Latest change: Fr Okt 30 05:23:42 CET 2015
contact: SvenG on Freenode ##sed or mailto:sed-freenode@guckes.net
thisfile: http://www.guckes.net/sed/seek.txt
in color: http://www.guckes.net/sed/seek.txt.html

= abstract =

i am looking for a short script (seek)
in awk or sed to look up some
note in a given file "FILE".

= assumptions =
the notes are separated by a line
"SEP" (---) from each other.

= example =

---
Do Okt 29 01:23:45 CET 2015
blah blah
---
Do Okt 29 07:30:00 CET 2015
more blah
foo
even more blah
---
Do Okt 29 07:32:38 CET 2015
moo!

the script should give me the complete note
(i.e. all lines between two separator lines)
when any of its lines matches a given regex
(or simply just some word "foo").

finally, this should work from the shell like this:

    $ seek foo
    ---
    Do Okt 29 07:30:00 CET 2015
    more blah
    foo
    even more blah

= Solutions =

by izabera:

  $ awk -v pattern=$1 '/^---$/ {
     if (section ~ pattern) print section; section = "---"; next }
     { if (length(section)) section = section "\n" $0; else section = $0
     }' $FILE

even shorter (also by izabera):

    $ awk /foo/ RS=--- $FILE

yay - i definitely like this really short version. :-)

(even though it does not yield the first separator
 line as i wanted. but this can be fixed easily.)

by causative:

    $ sed -e '/^---$/!{H;$!d};x;/foo/!d' $FILE

works! :)

the following two also work fine:

from Riviera:

"for many seds":

    $ sed '/^---$/!{H;d;};x;/foo/!d'

"and for anally conforming posix":

    $ sed -e '/^---$/!{' -e H -e d -e '}' -e 'x;/foo/!d'

izabera, causative, and Riviera - thank you all! :-)

--Sven

= result =

so now i have three more functions in my zsh setup:

    SIGS=~/.P/sig/sven.signatures # note: i am using "-- " as separators
    seek  () { awk /$1/ RS='-- ' $SIGS }                                  # from izabera
    seek2 () { sed "/^-- $/!{H;$!d;};x;/$1/!d" $SIGS }                    # from causative
    seek3 () { sed -e '/^-- $/!{' -e H -e d -e '}' -e "x;/$1/!d"  $SIGS } # from Riviera

EOF