0

I have multiple log files, which has format as below -

A.txt

18-05-23 11:28:25.939 ##Start
18-05-23 11:28:25.940 Unique ID - ABC123
18-05-23 11:28:25.941 ##End
18-05-23 11:28:25.942 ##Start
18-05-23 11:28:25.943 Unique ID - PQR123
18-05-23 11:28:25.944 ##End
18-05-23 11:28:25.945 ##Start
18-05-23 11:28:25.946 Unique ID - MNO123
18-05-23 11:28:25.947 ##End

B.txt

18-05-23 11:28:25.949 ##Start
18-05-23 11:28:25.950 Unique ID - ABC123
18-05-23 11:28:25.951 ##End
18-05-23 11:28:25.952 ##Start
18-05-23 11:28:25.953 Unique ID - PQR123
18-05-23 11:28:25.954 ##End
18-05-23 11:28:25.955 ##Start
18-05-23 11:28:25.956 Unique ID - MNO123
18-05-23 11:28:25.947 ##End

Here, I want to create C.txt file which contains only ABC123 information starting from ##Start line to ##End line also in sorted order. check below expected output for detailed understanding.

Expected C.txt

18-05-23 11:28:25.939 ##Start
18-05-23 11:28:25.940 Unique ID - ABC123
18-05-23 11:28:25.941 ##End
18-05-23 11:28:25.949 ##Start
18-05-23 11:28:25.950 Unique ID - ABC123
18-05-23 11:28:25.951 ##End

Please suggest- how can I achieve this using shell scripting ?

Girish K
  • 758
  • 1
  • 13
  • 24
  • Will it always be 3 lines: "Start", then "Unique ID", then "End"? Or will there sometimes be extra lines in the middle? If it's always 3 lines, then you can do a `grep` and `sort`, like this: `grep -A 1 -B 1 ABC123 A.txt B.txt | sort > C.txt` – Tim Johns May 25 '18 at 11:44
  • @TimJohns thanks for response. There will be extra lines in the middle. I have just given example of 3 lines. – Girish K Jun 05 '18 at 05:01
  • If the next pattern starts before the end of a pattern from another log file, do you want A1 A2 A3 B1 B2 B3 or A1 A2 B1 A3 B2 B3 ? (assuming A1=#start, A2=#Unique Id, A3=#end from a.txt, B1=#start, B2=#Unique Id, B3=#end from b.txt, and the B1 timestamp occurs between A2 and A3 ? – FBergo Jun 05 '18 at 05:17
  • I can't think know any way to do this without resorting to a more powerful programming language (Perl or Python are the usual candidates for this) or a special utility for multiline matching not always present in a standard Unix installation ( such as pcregrep, mentioned here: https://stackoverflow.com/questions/152708/how-can-i-search-for-a-multiline-pattern-in-a-file ) – FBergo Jun 05 '18 at 05:22
  • 1
    This is a Perl implementation of what you want: https://github.com/fbergo/examples/blob/master/logpat.pl , which will sort in A1 A2 A3 B1 B2 B3 order. If you want the other ordering, just pass the output file through `sort` on the command line. – FBergo Jun 05 '18 at 05:56
  • @FBergo There will be no scenario where B1 comes in between A2 & A3. Pattern will be A1=#start, A2=#Unique Id, A3=#end every time. Also,your implemented Perl script is very nice. but it is not working in case of below scenario => 18-05-23 11:28:25.939 ##Start 18-05-23 11:28:25.940 Some text Unique ID - ABC123 Sometext 18-05-23 11:28:25.941 ##End – Girish K Jun 06 '18 at 03:57

0 Answers0