Cygwin shell script

I have a file with below format :

2013-05-20  hello
has done
2013-05-20  hi
000abc
abc
2013-05-21 thank you

I want the out put to be like this :

2013-05-20  hello has done
2013-05-20  hi 000abc abc
2013-05-21 thank you

What I could think of is something like this :

cat input.txt | sed -r ':a ;$! N; s/\n([0-9]{4}-[0-9]{2}-[0-9]{2})/ \1/; ta ; P ; D' >> result.txt;

Problem with the above script is it does this :

2013-05-20  hello
has done 2013-05-20  hi 
000abc
abc 2013-05-21] thank you

That means I need to put a not(!) in the date format

(([0-9]{4}-[0-9]{2}-[0-9]{2})

. Kindly help!

Thanks in advance

If you assume all line with 2013 are years. (you can modify regex to fit your need)

awk '/^2013/ {print s;s=$0} !/^2013/ {s=s" "$0} END {print s}'

2013-05-20 hello has done
2013-05-20 hi 000abc abc
2013-05-21 thank you

Really thanks for the reply!

Here in my example we are assuming the date to be 2013 but it can be anything. So it would be better if we consider the complete date 2013-05-20 as pattern.
Also as I am new to shell scripting can you please let me know how can I output the result in a file?

awk '/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/ {print s;s=$0} !/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/ {s=s" "$0} END {print s}'

Less precise version

awk --posix '/^[0-9-]{10}/ {print s;s=$0} !/^[0-9-]{10}/ {s=s" "$0} END {print s}' file

This will test if first 10 characters are 0 to 9 or -
You need this --posix , -W posix or this --re-interval option to make {x} work

Thanks a lot :slight_smile:

Slight change to simplify and remove blank fist line:

awk '/^[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]/{if(s)print s;s=$0;next} {s=s" "$0} END {print s}'
1 Like

Thanks for the reply. I had already put one sed command to remove all empty line, but this code to remove the first line looks great.

Thanks Chubler.
Then next saves testing for not

The posix version

awk --posix '/^[0-9-]{10}/{if(s)print s;s=$0;next} {s=s" "$0} END {print s}'