0

I am trying to grep the line of specific pattern from log file Logfile.txt and redirecting the output to new text file 1.txt. Log file has some control ^M character in it and also those lines will be available between the timestamp lines, so im trying to merge all those multiple lines into single line and trying to append it as shown in the expected result if the previous line contains Execute pattern in it. But couldn't able to get it as expected

Logfile.txt

    1993-01-04 06:24:03,068 INFO  b: [Cool: read-189231]: Opening the File
    1993-01-04 06:24:13,068 INFO  b: [Cool: read-189231]: Checking the content
    1993-01-04 06:24:23,148 INFO  b: [Cool: read-189231]: Setting the session:
    1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Checking the content
    1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Compiling the query
    1993-01-04 06:24:27,148 INFO  ab: [Cool: read-189231]: Completed
    1993-01-04 06:25:22,168 INFO  ba: [Cool: read-190983]: Execute ^M
    I am just checking the answer ^M
    Need your support for the workaround ^M
    This log has some control character ^M
    1993-01-04 06:25:22,168 INFO  a: [Cool: read-190983]: Main Execution completed
    1993-01-04 06:25:52,188 INFO  ba: [Cool: read-190983]: Execute the line : How are you
    1993-01-04 06:26:45,268 INFO  a: [Cool: read-190983]: Exiting

Code - My Findings

egrep -wah 'Setting the session:|Checking the line|Completed|Execute|Exiting' Logfile.txt > 1.txt

Expected Result

1993-01-04 06:24:23,148 INFO  b: [Cool: read-189231]: Setting the session:
1993-01-04 06:24:25,068 INFO  b: [Cool: read-189231]: Checking the line
1993-01-04 06:24:27,148 INFO  ab: [Cool: read-189231]: Completed
1993-01-04 06:25:22,168 INFO  ba: [Cool: read-190983]: Execute ^M I am just checking the answer ^M Need your support for the workaround ^M This log has some control character ^M
1993-01-04 06:25:52,188 INFO  ba: [Cool: read-190983]: Execute the line : How are you
1993-01-04 06:26:45,268 INFO  a: [Cool: read-190983]: Exiting
2
  • Where is the Main Execution completed gone? Do you really want the ^M kept in output (they'll overwrite each precedent output line)?
    – RudiC
    Apr 21, 2020 at 7:20
  • If you see I used to grep only the lines which has some patterns, I dont need other lines which can be ignored
    – user78873
    Apr 21, 2020 at 7:52

1 Answer 1

0

Try

awk '{X = $0; while (/\r$/) {getline; if (! /^[0-9]/) X = X $0}; gsub (/\r/, "", X); print X} 1 ' file

When encountering a ^M char, it loops reading and adding more lines until a date is read, then prints the entire line.

1
  • no it is not working out for me.
    – user78873
    Apr 22, 2020 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.