Append lines for a specific pattern

Input:

09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text

Output:

09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

The rule is to append all lines so each line contains this format

\d{2}:\d{2}:\d{2},\d{3}

Thx in advance.

Try:

awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

A lazy way ...

sed '2,$s/^[0-9]/#&/g' input | xargs | tr '#' '\n'

---------- Post updated at 11:42 AM ---------- Previous update was at 11:35 AM ----------

@Scruti

I tried your code on a linux machine : the result is still printed in only one line

$ cat input
09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text
$ awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text10:45:46,538 INFO second text11:00:46,538 INFO third more text
awk '/INFO/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

Or

awk '/^[0-9]/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

Guru.

@ctsgnb: good point. Try:

awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

or use:

awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= infile
1 Like

@Scruti :

The second one works fine (on a Ubuntu 11.04) :

$ awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
awk: not an option: --posix
$ awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

---------- Post updated at 01:36 PM ---------- Previous update was at 01:20 PM ----------

awk '/INFO/&&NR>1{print z}{printf $0}END{print z}' input

@ctsgnb, it is best to disable the format field in printf to avoid unintended interpretation of possible formatting characters in the input.

awk '/INFO/&&NR>1{print z}{printf "%s",$0}END{print z}' input
1 Like

In case you don't need to handle a stream...

printf 'v/INFO/-,j\nx\n' | ex file

Regards,
Alister