5

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.

3 Answers 3

6

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

3
  • 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?
    – krizzo
    Sep 24, 2011 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, 2011 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.
    – krizzo
    Sep 26, 2011 at 15:00
4

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
2
  • 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
    – krizzo
    Sep 24, 2011 at 23:08
  • Don't forget to escape the round bracket with a backslash.
    – quanta
    Sep 25, 2011 at 3:08
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .