sed script to parse logs issue

I have this script to parse some logs:

#!/bin/bash
id=$1
shift
sed "/(id=$id)/,/^$/!d" "$@"

Usage: ./script.sh 1234 logfile

The logs have an empty line before the logged events/timestamps -- most of the time. And this is my issue, since when there is no empty line, it will catch things I don't want.

An example of the logs:

test@server 2012-11-12 10:00:00 (id=1234)
text
text
text

test@server 2012-11-12 10:01:00 (id=1234)
more text
more text
more text
test@server 2012-11-12 10:02:00 (id=4321)
even more text
even more text
even more text

Here my script would also return the events for id 4321, which I do not want.

Any great ides on how to solve this?

If you don't mind some extra new-lines:

sed 's/^test@server/\
&/' file|sed "/(id=$id)/,/^$/!d"
1 Like

if every tpeserver contains only 3 lines after it, then you can go for this

sed -n "/id=${id}/ {N;N;N;p}" input_file

That solved my issue, thanks! :slight_smile:

---------- Post updated at 03:02 PM ---------- Previous update was at 03:01 PM ----------

No they don't, but thanks for the input. :b:

awk version:

awk '/id=/{print RS}1' "$@" | awk "/id=$id/" RS=