LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line) (http://www.linuxquestions.org/questions/programming-9/bash-grepping-sedding-etc-out-part-of-a-file-from-one-word-to-blank-line-689855/)

elinenbe 12-11-2008 12:04 PM

BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line)
 
I have a file that lists data about a system. It has a part that can look like:

the errors I'm looking for with other errors:
Code:

Alerts
 Password Incorrect
 Login Error
 Another Error
 Another Error 2
 
Other Info

or, just the errors I need to parse for:
Code:

Alerts
 Password Incorrect
 Login Error
 
Other Info

or, if no alerts:
Code:


Other Info

What I need to do is parse the file, and branch iff the error "Password Incorrect" or "Login Error" comes up. If those come up with other errors that is fine. The file doesn't always include errors (actually this is usually the case). Also, what makes this difficult for me is the line after the last alert isn't blank -- it actually contains a single space.

I have code for it, but it's getting messier and messier, and I'm sure you guys help me out with something WAY more elegant.

Thanks,
Eric

syg00 12-11-2008 12:14 PM

Quote:

Originally Posted by elinenbe (Post 3371901)
I have code for it, but it's getting messier and messier

My natural reaction if I get to this point is to reach for perl.

elinenbe 12-11-2008 01:17 PM

here's what I have
 
So, this is what I've got so far...

Code:

awk '/^Alerts/,/^Other Info/' myfile >myfile1
sed 'N;$!P;$!D;$d' myfile1 > myfile2
sed '1d' myfile2 > myfile3
if [[ `cat myfile3 | grep -E 'Password Incorrect|Login Error'` ]]
then
    DO CODE HERE
fi



All times are GMT -5. The time now is 07:06 AM.