Click to See Complete Forum and Search --> : Bash scripting question...regular expression


jrap
02-19-2005, 10:02 PM
Hi folks. I'm pretty new to bash scripting, and could really use your help with the following:

I have a file that I need to parse, and I need to get the contents in between "\nMessage:\n" and the next "\nMessage:\n".
There will be multiple messages I need to get. How would I go about doing this?

So the file could look like this:


Message:
a
b
c
d
Message:
1
2
3
4
Message:
l
i
n
u
x


Thanks!

chrism01
02-21-2005, 11:03 PM
Here's a quick n dirty:


for line in `cat t.t`
do
t=`echo $line|grep -i Message`
if [[ $? -ne 0 ]]
then
echo $line
fi
done


NB this only works for files eg t.t where there are
no spaces in the lines as cat splits on spaces.

This will work for lines with spaces:

grep -v Message t.t