finding lines only between a certain string

Dear experts,

Ive been trying to figure this out for a while, but i cant. Please help.
I have a file, with approx 1 million lines. The contents are separated with "----------". Please see example below

So my problem is, i need to find all texts that have the keyword "GAA", but i need to see everything that is between the --------- lines. Desperatedly need a script to do this. Any help is appreciated. Thanks !

Can you provide sample expected output.

A sample expected output should be all lines in the file, which match the search pattern "GAA", and output in the format below

Try this:

awk '
        # new record, reset array index
        /^----/ { i=0 }
        # accumulate record contents in array
        { a[++i]=$0 }
        # matching record
        /GAA/ {
                # dump array contents
                for (j=1; j<=i; j++)
                        print a[j]
                # get the rest of this record
                while (getline && $0 !~ /^----/)
                        print
                # print the record terminator and reset the array
                print
                i=0
        }
' inputfile > outputfile

Annihilannic,

Tried the script, it took about 3 mins to run, but no output. outputfile was empty :frowning:

Strange, works fine for me with the sample data you provided. What operating system are you using? Are there any spaces at the beginning of the lines?

The file starts like this

Im using solaris 9

What's the output of head -50 /tmp/AIALARM_MSNLA_20080626.txt | cat -vet? Can you post it between [ code ] tags instead of [ quote ] tags please?

oh dear im getting a wierd result. the file is filled with ^M. Im sorry ill ftp the file out again. Thanks

root@ckpgpay11core> head -50 AIALARM_MSNLA_20080626.txt | cat -vet
Mroot@ckpgpay11core>

No big deal, it just means the file is in DOS/Windows format. You can use dos2unix to convert it (if available on your system), or tr -d '\r' < dosfile > unixfile.

Thanks guys

Dear Annihillanic,

I got the script working. Sorry but i have another question. I am making the script s little interactive, and have modified it to search for different strings everytime i run it. I cant seem to replace the variable "GAA" in the script. Any idea why? below if the output of the script when run in debug mode

root@ckpgpay11core> ./sarascript.sh
+ echo Enter input filename:
Enter input filename:
+ read inputfile
mstgb_aialarm_20081021.wri
+ echo Enter path of outputfile with name:
Enter path of outputfile with name:
+ read outputfile
/tmp/testing
+ echo Enter tag to look for:
Enter tag to look for:
+ read GAA
20081013003507
+ awk
        # new record, reset array index
        /^----/ { i=0 }
        # accumulate record contents in array
        { a[++i]=$0 }
        # matching record
        /\$GAA/ {
                # dump array contents
                for (j=1; j<=i; j++)
                        print a[j]
                # get the rest of this record
                while (getline && $0 !~ /^----/)
                        print
                # print the record terminator and reset the array
                print
                i=0
        }
 mstgb_aialarm_20081021.wri

root@ckpgpay11core> ls -la | grep testing
-rw-r--r--   1 root     other          0 Oct 30 10:47 testing

perl

undef $/;
open FH,"<file";
$str=<FH>;
@arr=split(/-+/,$str);
for $key(@arr){
if($key=~/pat/){
print $key;
}
}
close FH;

Because the awk script is between single quotes, shell variables are not expanded.

You can change it like this to allow it to interpolate the variable:

        /'"$GAA"'/ {

Or else use :

awk -v SEARCHSTRING="$GAA" '
        ...
        $0 ~ SEARCHSTRING {
        ...
' inputfile > outputfile

thanks guys. works perfectly