Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding pattern with sed, print lines until first empty line

I am trying to find a specific PID in a logcat and print all lines until the next empty line. The logcat looks something like this.

[ 07-17 11:15:14.003   483:  515 D/SensorService ]
Message

[ 07-17 11:15:15.300 20220:20225 D/dalvikvm ]
Message

[ 07-17 11:15:17.519   483:  515 D/SensorService ]
Message

What I have found searching for a solution to this problem is this:

sed -n '/pattern/,/^$/p' logcat

In this example I would like to do this

sed -n '/20220/,/^$/p' logcat

However, I cannot get this to work. It actually prints the entire logcat instead of filtering it. When I do execute

sed -n '/pattern/p' logcat

it works just as expected (like grep) but only taking the first line. In my understanding ^ represents the beginning of a line and $ the end, thus making ^$ an empty line.

I would be grateful for any inputs in how to solve this problem. Thank you.

like image 557
Oskar Avatar asked Mar 19 '23 00:03

Oskar


1 Answers

Through awk,

$ awk '/20220/{p=1}/^ *$/{p=0}p' file
[ 07-17 11:15:15.300 20220:20225 D/dalvikvm ]
Message

Through sed,

$ sed -n '/20220/,/^ *$/p' file
[ 07-17 11:15:15.300 20220:20225 D/dalvikvm ]
Message

But the above sed command prints also the blank line.

like image 196
Avinash Raj Avatar answered Apr 06 '23 12:04

Avinash Raj