0

I have some log files with some information as below

2016-03-18 00:07:43,482 SOME ERROR 
OCCURED
API 
LINE INFO
2016-03-18 00:07:44,482 OCCURED
authentication failure
2016-03-18 00:07:45,482 ERROR OCCURED
2016-03-18 00:07:46,482 NOT IMP OCCURED

Desired Output

2016-03-18 00:07:43,482 SOME ERROR OCCURED API LINE INFO
2016-03-18 00:07:44,482 OCCURED authentication failure
2016-03-18 00:07:45,482 ERROR OCCURED
2016-03-18 00:07:46,482 NOT IMP OCCURED

That means I want to merge all the lines between 2016-03-18

I want every line to start with the 2016-03-18 as the pattern.

Could someone help me do this using shell scripting preferably using awk/Sed?

Deepak Kumar
  • 1,669
  • 3
  • 16
  • 36

2 Answers2

2

You can use the following awk command:

awk '
    /^[0-9]{4}(-[0-9]{2}){2}/ && line {print line; line=""}
    {line = line ? line" "$0 : $0}
    END {print line}
' file 

Update

It turned out that some awk implementations doesn't support {n} quantifiers. For example mawk.

In that case you'll have to write the command like this:

mawk '
    /^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/ && line {print line; line=""}
    {line = line ? line" "$0 : $0}
    END {print line}
' file
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • This isn't working for me. i have used this awk ' /^[0-9]{4}(-[0-9]{2}){2}/ && line {print line; line=""} {line = line ? line" "$0 : $0} END {print line}' tt.txt >tt1.txt – Deepak Kumar Mar 19 '16 at 11:02
  • @JavaTechy Looks like your implementation of `awk` does not support `{n}` quantifiers. I've added a workaround. – hek2mgl Mar 19 '16 at 11:13
  • this is also not working.I am new to awk. I think this can help u. This question is similar . but i dont know what to change. http://stackoverflow.com/questions/21099468/how-to-merge-unknown-number-of-multiple-lines-between-two-patterns-into-a-single – Deepak Kumar Mar 19 '16 at 11:21
  • Being new to something is not a problem but also no excuse. "this is also not working" is not a helpful error report. I obviously can't jump into your computer and check what is "not working". What is your awk version? – hek2mgl Mar 19 '16 at 11:27
  • Don't get angry. Its fine. i will try more into it. thanks for the help. its 1.3.3 – Deepak Kumar Mar 19 '16 at 11:31
  • Again, what is your awk version? – hek2mgl Mar 19 '16 at 11:43
  • this is the version mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan – Deepak Kumar Mar 19 '16 at 11:44
  • Hmm, the second example I gave has been explicitly tested against that version. Are you using the sample data from the question or something different? – hek2mgl Mar 19 '16 at 11:46
  • the data is same. the only difference is that i am writing output to a file and then checking it. And in command line also the result is same – Deepak Kumar Mar 19 '16 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106794/discussion-between-javatechy-and-hek2mgl). – Deepak Kumar Mar 19 '16 at 12:01
0

This might work for you (GNU sed):

sed ':a;N;/\n....-..-.. /!s/\n/ /;ta;P;D' file

Read two lines into the pattern space and if the second line does not match the pattern that starts a line replace its preceeding newline by a space and repeat. Otherwise print and then delete the first line and repeat.

potong
  • 55,640
  • 6
  • 51
  • 83