search pattern and read lines

Hi,

I have a huge de-limited file which has pattern :

99"9876"2010-11-21 12:51:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0         "" <<read>>
99"9876"2010-11-21 12:51:01"K"R-EMP# 01234567      (LOGOFF)               "" <<read>>
99"9876"2010-11-21 12:51:01"L"  *AUTO LOGOFF*                             "" <<read>>
99"9876"2010-11-21 12:51:01"M"END OF XXXXXXXXXXX #5180   END:12:51 PM     "" <<read>>
99"9876"2010-11-21 12:51:01"N"-2798    0        0        418      0       "" <<read>> 
99"1234"2010-11-21 12:12:12"J"MNOPQRS ID# 2-1234-1234-0099-1234-0         "" <<read>>
99"1234"2010-11-21 12:12:12"K"R-EMP# 01234567      (LOGIN)                "" <<read>>
99"1234"2010-11-21 12:12:12"L"  *AUTO LOGIN COMPLETED*                    "" <<do not read>>
99"1234"2010-11-21 12:12:12"M"END OF XXXXXXXXXXX #5180   END:12:12 PM     "" <<do not read>>
99"1234"2010-11-21 12:12:12"N"-2798    0        0        418      0       "" <<do not read>> 
99"5678"2010-11-21 12:10:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0         "" <<read>>
99"5678"2010-11-21 12:10:01"K"R-EMP# 01234567      (OPEN)                 "" <<read>>
99"5678"2010-11-21 12:10:01"L"  *AUTO LOGOFF*                             "" <<do not read>>
99"5678"2010-11-21 12:10:01"M"END OF XXXXXXXXXXX #5180   END:12:10 PM     "" <<do not read>>
99"5678"2010-11-21 12:10:01"N"-2798    0        0        418      0       "" <<do not read>>

I'm reading each line (while loop) and parsing the values to an output file based upon column-4.
When it reads 'K' type of line it extracts the value within the brackets in the last column(here, LOGOFF).

I need to achieve this:
If it is equal to LOGOFF or TURNOFF or CLOSE read the next lines as usual continously. If it is not equal to LOGOFF or TURNOFF or CLOSE, do not read the next lines that have the same combination of values present in column-1,2,3.
Here, do not read a line that has values
'column-1=99' && 'column-2=1234' && 'column-3=2010-11-21 12:12:12'

Using grep for searching the pattern and break for not reading the line is prefered.

  1. What do you want to do to the lines "read"?
  2. Why would you not want to "read" the lines marked as 'do not read'?

Hi,
the text <<read>> and <<do not read>> are not part of the input file. That is why I highlighted with red.
The lines <read> are to be parsed to another output file.
<do not read> have to be skipped to be parsed to the output file

Try this:

perl -F'"' -lane 'BEGIN { open O, "> outputfile.txt" }
if ($F[3] eq "K" && $F[4] =~ /LOGOFF|TURNOFF|CLOSE/) {
    $f = 0;
    print O;
}
elsif ($F[3] eq "K" && $F[4] !~ /LOGOFF|TURNOFF|CLOSE/) {
    $f = 1;
    print O;
    $a = $F[0]; $b = $F[1]; $c = $F[2];
}
elsif ($f == 1 && $F[0] == $a && $F[1] == $b && $F[2] eq $c) {
    next;
}
else {
    print O;
}
END { close O }' inputfile.txt