Multiple lines into a single line on Ubuntu 10.04

Hi,

I've some files with the following data and i need to convert the lines between the separator ---, into a single line. I've tried with the paste cmd but my main problem is that the number of lines between the separator is not fix, it can vary between 1-4 lines.

Input

---
2010-02-22 12:25:32
191
---
2010-02-22 12:25:32
---

Output

--- , 2010-02-22 12:25:32 , 191
--- , 2010-02-22 12:25:32

Sometime ago i posted this message and got the following solution:

awk 'END{print _}/^-/{if(_)print _;_=$0;next}{_=_" , "$0}' infile

My problem is that this solution only works on Ubuntu 9.10 and i recently upgraded my machine to Ubuntu 10.04, and when i execute the awk command it only returns the last line before the separator (---).

Amy ideia why this happens?

Thanks in advance

This works for me:

awk '
        function p()
        {
                if( buffer )
                        print "---" buffer;     # print if it existed; dont print for last seperator
        }

        /^---/ {
                p();            # print if not first
                buffer = "";    # reset
                next;
        }

        { buffer = buffer ", " $0; }    # add lines to buffer

        END { p(); }            # handle case of no terminating separator
' <input-file
1 Like

I'd be interested to know why the original awk script fails in Ubuntu 10.04?

try gawk.

may be in the new s/w version's awk is the broken one.

gawk 'END{print _}/^-/{if(_)print _;_=$0;next}{_=_" , "$0}' infile

mawk works with the script in post#1, gawk does not. They both work if you use a letter instead of an underscore:

awk 'END{print p}/^-/{if(p)print p;p=$0;next}{p=p" , "$0}' infile

With 10.04 the default awk changed from mawk to gawk on Ubuntu.

1 Like

It's true if i execute the command with mawk it works, but if i use awk it doesn't.

One more thing the command should join all lines between the separator into one, and instead is creating 2 lines any idea why.

Source File:

-------
2010-10-18;10:55:50;AV;0594  
F0594
Windows NT Versi�n 4.0  
lun 18/10/2010 
10.55 
-------
2010-10-18;10:55:56;AV;0591  
F0591
Windows NT Versi�n 4.0  
lun 18/10/2010 
10.55 
-------

Current output

(mawk 'END{print _}/^-/{if(_)print _;_=$0;next}{_=_";"$0}' infile)

-------;2010-10-18;10:55:50;AV;0594 
;F0594;Windows NT Versi�n 4.0  ;lun 18/10/2010 ;10.55 
-------;2010-10-18;10:55:56;AV;0591 
;F0591;Windows NT Versi�n 4.0  ;lun 18/10/2010 ;10.55 

Desired output (single row):

-------;2010-10-18;10:55:50;AV;0594 ;F0594;Windows NT Versi�n 4.0  ;lun 18/10/2010 ;10.55 

Thanks for the help

I can't reproduce this. I get:

$ mawk 'END{print _}/^-/{if(_)print _;_=$0;next}{_=_";"$0}' infile
-------;2010-10-18;10:55:50;AV;0594  ;F0594;Windows NT Versi�n 4.0  ;lun 18/10/2010 ;10.55
-------;2010-10-18;10:55:56;AV;0591  ;F0591;Windows NT Versi�n 4.0  ;lun 18/10/2010 ;10.55
-------
1 Like

Hi,

A 'sed' solution:

sed -n '/^---/ { x ; /^---/ { s/^\n// ; s/\n\+/ , /g ; p } } ; /^---/! { s/ *$// ; H }' infile

Regards,
Birei

1 Like

thanks a lot for all the help.