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:
Alerts
Password Incorrect
Login Error
Another Error
Another Error 2
Other Info
or, just the errors I need to parse for:
Alerts
Password Incorrect
Login Error
Other Info
or, if no alerts:
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.
I have some thoughts by using sed, tr, awk, etc...
But, cannot put to paper without seeing more of the input data and also what you are trying to get as output.
[LEFT]more of the input would look something like:
Time
12:34 PM
Alerts
Password Incorrect
Login Error
Another Error
Another Error 2
Other Info
Memory Status 12
CPU Status 12.44
Bandwidth
SATA 57%
ATA 12%
Processes
Process Memory CPU
AAA 12.4GB 34%
BBB 8.4GB 17%
> sed "s/^[A-Z]/~&/" file110 | tr "\n" "|" | tr "~" "\n" | egrep "^Alerts|^Other Info" | tr "|" "\n"
Alerts
Password Incorrect
Login Error
Another Error
Another Error 2
Other Info
Memory Status 12
CPU Status 12.44
file110 is simply the log file I copied your sample to
the first sed marks the major headings
then convert new-lines to | (as delimiter)
force new-lines before major headings
grep for either of two major headings
convert the | back to new-lines
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
var1=`sed -ne '/Alerts/{
N
/Password Incorrect/!d
}
/Alerts/,/Other Info/p
' filename`
var2=`sed -ne '/Alerts/{
N
/Login Error/!d
}
/Alerts/,/Other Info/p
' filename`
if [ "$var1" -a "$var2" ]
then
DO CODE HERE
fi