Merging the lines of a file

Hello,

I have a file with few lines starting with a digit (1-5 only ) followed by a dot (.). Remaining all the lines to be merged with its previous numbered lines. Merging must be done with a space.

E.g.,
Source file:

3. abc def
xyz
5. pqr mno
def
4. jkl uvw
7. ghi
1. abc xyz
6. mno
9. kkk
3. fff dddd
2. ddd eeee
jjj

Output file:

3. abc def xyz
5. pqr mno def
4. jkl uvw 7. ghi
1. abc xyz 6.mno 9. kkk
3. fff dddd
2. ddd eeee jjj

In the end, all the lines must start with a digit 1-5. Pls note that other digits [6-9] have been merged with its prev lines.

Please help me, this is not homework, thank you.

Try

awk '!/^[1-5]/ {X=X" "$0; next} X {print X} {X=$0} END {print X}' file
3. abc def xyz
5. pqr mno def
4. jkl uvw 7. ghi
1. abc xyz 6. mno 9. kkk
3. fff dddd
2. ddd eeee jjj

Thanks RudiC for the reply.

It works but with some issues like it's checking just first digit. It should rather check with "dot" as well like "1." "2." .... "5.". Otherwise it considering 11 also as starting field where actually the lines starting with 11 should be merged with previous lines.

Please advise!

Magnus29,

You just need to add dot in Rudic code for pattern matching like below.

awk '!/^[1-5]\./ {X=X" "$0; next} X {print X} {X=$0} END {print X}' file

Another awk

awk '{printf (/[0-5]\./?RS:FS)"%s",$0}' file

3. abc def xyz
5. pqr mno def
4. jkl uvw 7. ghi
1. abc xyz 6. mno 9. kkk
3. fff dddd
2. ddd eeee jjj