Bash – Regex for sed to grab multiple lines or a better way

bashregexsed

I'm creating a script that connects to a server and dumps the output to a tempfile. I want to use sed in the script to grab specific information from the temp file. The output would always have the 80 char dashed line then the information I want followed by the Disconnected statement.

I've gotten a regex working if it was just a single line the trouble is how do I group the newlines as well?

Regex

-\{80\}[\r\n]*\(.*\)[\r\n]\{4\}Disconnected

File

...
--------------------------------------------------------------------------------
The information that I want to get can be a single line or multiple lines.
Another line to grab.

And this should be caught as well.

Disconnected ...

Desired output

The information that I want to get can be a single line or multiple lines.
Another line to grab.

And this should be caught as well.

Best Answer

First use the '-n' flag to suppress automatic output. Next use sed addresses to quote the part you are interested at (from the dashes "---" till a line that has the word "Disconnected"). Finally print the pattern space (all the pattern space since you're interested in everything inside it).

~$ sed -n '/^---*/,/Disconnected/{p}' inputfile

Edited because of LF4 request of removing the line with dashes from the result.

With the "addresses" you quote individual pattern spaces. So you can do whatever you want with those individual pattern spaces. Including remove lines by regexp. In the example, the command removes lines formed by dashes from the pattern space yielding the output you're looking for:

~$ sed -n '/^---*/,/Disconnected/{/^---*/d;p}' inputfile

HTH