Complex search and output

Hi,

I have somewhat of a complex task at hand. I am on a HP-UX box and I need to search for information embedded in files, further embedded into a file/folder structure.

I compiled a list of information that I need found:

File=list.csv

DATE FIELD 1 FIELD2
08/13/09,EMCU9629413,YMLZ4807940
08/13/09,TCKU9085252,CCLZ400809
08/14/09,EMCU9678634,FLXZ412869

Directory Structure

/edi/backup/out/done/ega/090812/multiple files
/edi/backup/out/done/ega/090813/multiple files
/edi/backup/out/done/ega/090814/multiple files
...and so on, and so on...

File Structure

CATOS2EDI....
CNTR...
CARR...
PORT...
DOC...
TRACE....

The above is one complete document. The starting segment of a document is "CATOS2EDI" and the ending segment of that same document is "TRACE". There are be multiple documents within one file.

FIELD1 & FIELD2 will always be in the same segment, the CNTR segment.

I need to:

  1. Use the DATE field in the file as a string to get the file from the write directory. i.e. Make a variable from the DATE field in the file: 08/13/09 ~> 090813
  2. Search for FIELD1 & FIELD2 in that directory. i.e. grep -e ^CNTR.*FIELD1.*FIELD2 /edi/backup/out/done/ega/$DTE
  3. Return only the document that FIELD1 & FIELD2 were found. i.e. Not sure how to return this information.
    NOTE: There can be more segments in between the CATOS2EDI and TRACE segment, but there is only one start segment with one end segment to indicate on complete document.

Expected Results:

3 Documents in 1 file:

CATOS2EDI....
CNTR...EMCU9629413YMLZ4807940
CARR...
PORT...
DOC...
TRACE....
CATOS2EDI....
CNTR...TCKU9085252CCLZ400809
CARR...
PORT...
DOC...
TRACE....
CATOS2EDI....
CNTR...TCKU9085252CCLZ400809
CARR...
PORT...
DOC...
TRACE....

NOTE: The output does not look like this exactly, but I just wanted to illustrate the starting (CATOS2EDI) and ending (TRACE) segments.

Please let me know if there is an easy way to do this or if you have any questions.

Thanks,
Chris

You can search on this forum for ways to reformat dates using grep, which is what you will need to get started on question 1 and 2. I am only answering to your query since your post is indeed rather complex and you are more likely to get reusable answers if you split up your issue in more digestible chunks. Or better even: post code that you are using and which does not work as expected.

Thanks for the advice. Is there a way to just delete this post?