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...
link|improve this question

possible duplicate of Regex for sed to grab multiple lines or a better way? – quanta Oct 4 '11 at 3:51
feedback

2 Answers

up vote 6 down vote accepted

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/

link|improve this answer
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. – LF4 Oct 3 '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 3 '11 at 17:19
feedback

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.

link|improve this answer
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 which no one has been able to answer. – LF4 Oct 3 '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 ? – Iain Oct 3 '11 at 20:06
feedback

Your Answer

 
or
required, but never shown

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