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?




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.

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


  • Thats nice and clean but it's also including the dashes and the disconnected line. I have to run lines=wc -l; sed -n '2;$lines {p}' in order to remove them is there a way to group with that sed command? – LF4 Sep 24 '11 at 23:13
  • Notice that with sed addressed you get the individual pattern spaces. With them you can do whatever you want. See my edit. – hmontoliu Sep 25 '11 at 10:25
  • Interesting thank you for explaining that I didn't know about sed addresses and how they worked I'll have to do reading about it. – LF4 Sep 26 '11 at 15:00

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
  • This worked well, I modified it to grab just the text excluding the dashes and the Disconnect as well as the blank line before Disconnected and after the dashes. sed -n '1h;1!H;${;g;s/.*(-\{80\}\n(.*)\nDisconnected.*/\1/p;}' file – LF4 Sep 24 '11 at 23:08
  • Don't forget to escape the round bracket with a backslash. – quanta Sep 25 '11 at 3:08

Easiest, but not very efficient way is

  1. Use tr to remove all newlines.

    tr '\n' ' '
  2. Re-add newlines after the Disconnected with sed's \a command.

  3. Parse that data using your sed command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.