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...
1
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/

2
  • 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
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.

2
  • 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 ? – user9517 Oct 3 '11 at 20:06

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.