3

I'm trying to create a bash script that will grep lines from a file using egrep. I've created the regex that should group the information I want, the issue is trying to get the output. I've been testing it with the following command but nothing is printed when ran. How do I print the multiple lines between the -{80} and Disconnected?

egrep -E "^-{80}$\r?\n?([:ascii:]*)Disconnected from Server" testing.txt

File: testing.txt

Connected to the server: name here

Some header text.
More text to go though...
--------------------------------------------------------------------------------
The information that I want, would be in here;

Including this line as well #$
and this one.

Disconnected from Server...
krizzo
  • 387
  • 2
  • 5
  • 16
  • possible duplicate of [Regex for sed to grab multiple lines or a better way?](http://serverfault.com/questions/315145/regex-for-sed-to-grab-multiple-lines-or-a-better-way) – quanta Oct 04 '11 at 03:51

2 Answers2

6

You might be better off using a tool like awk.

awk '/^----+$/ {flag=1;next} /Disconnected from Server/{flag=0} flag {print}'

See: http://nixtip.wordpress.com/2010/10/12/print-lines-between-two-patterns-the-awk-way/

Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • Thanks I figured I'd try grep first since I've used it instead of awk but that works better. I'll have to learn about awk. – krizzo Oct 03 '11 at 17:16
  • 1
    AFAIK, all the *grep work only line-by-line, but I could be wrong. If you need to work on ranges, you have to use awk, perl, or something else. – Zoredache Oct 03 '11 at 17:19
1

Just because I eventually worked it out here's a sed version

sed -n '/^-----\+$/,/^Disonnected/ {/^----\+$/d;/^Disonnected/d;p;}' testing.txt

This operates on all lines between /RE1/ and /RE2/, if the input matches /RE1/ or /RE2/ then it is deleted otherwise it is printed.

user9517
  • 115,471
  • 20
  • 215
  • 297
  • sed wont work because I'm doing this on Linux and AIX systems and sed seems to be functioning odd with the AIX. See my question [here: ServerFault](http://serverfault.com/questions/317395/aix-sed-parsing-error-0602-404-but-works-with-linux-sed) which no one has been able to answer. – krizzo Oct 03 '11 at 19:35
  • @LF4: I don't have an AIX box to test it on, but it does work on Solaris if you simplify the inital RE to `/^-------/` or `/^-\{80\}$/` or similar. Can you try it ? – user9517 Oct 03 '11 at 20:06