Regex for sed to grab multiple lines or a better way?
Solution 1:
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
Solution 2:
sed
can look for the pattern in multiple lines by concatenating them into which is called "hold space", something like this:
$ sed -n '1h;1!H;${;g;s/.*\(-\{80\}.*Disconnected\).*/\1/p;}' file
-
1h
: copy the first line to hold space -
1!H
: from the second line, append to hold space -
$
: the last line -
g
: copy the hold space to pattern buffer -
s/pattern/substitution/
: search and substitute -
\1
: back reference to the group in pattern -
p
: print
Solution 3:
Easiest, but not very efficient way is
-
Use tr to remove all newlines.
tr '\n' ' '
Re-add newlines after the
Disconnected
with sed's\a
command.- Parse that data using your
sed
command.