Generating CSV from Column data

Hi List,

I have a chunk of data like so:

User Account Control:
User Account Control:
User Account Control:
User Account Control:
        Disabled
User Account Control:
User Account Control:
User Account Control:
        Disabled
User Account Control:
User Account Control:
        Disabled

What I want to do is collapse the Disabled row to the end of the row above into a CSV like so:

User Account Control:
User Account Control:
User Account Control:
User Account Control:,Disabled
User Account Control:
User Account Control:
User Account Control:,Disabled
User Account Control:
User Account Control:,Disabled

Any ideas?

thanks ./

As long as there is only one word on indented lines (as in your example), the following should work:

awk '
/^[[:space:]]/ {
        o = o "," $1
        next
}
{       if(o != "") print o
        o = $0
}
END {   if(o != "") print o
}' file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

1 Like

You may try this also

$ cat <<eof | awk '{S=!/^[[:space:]]/? S RS $0 : S OFS $1}END{print S}' OFS=\,
User Account Control:
User Account Control:
User Account Control:
User Account Control:
        Disabled
User Account Control:
User Account Control:
User Account Control:
        Disabled
User Account Control:
User Account Control:
        Disabled
eof

User Account Control:
User Account Control:
User Account Control:
User Account Control:,Disabled
User Account Control:
User Account Control:
User Account Control:,Disabled
User Account Control:
User Account Control:,Disabled
1 Like

try also:

awk '!/:/ {sub("\n$", "", s); $0="," $1} {s=s $0 "\n";} END {printf s}' input
1 Like