# Generating CSV from Column data

**URL:** <https://community.unix.com/t/generating-csv-from-column-data/336216>\
**Category:** Shell Programming and Scripting\
**Created:** [November 3, 2013, 11:34pm UTC](https://community.unix.com/t/generating-csv-from-column-data/336216 "2013-11-03T23:34:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/6_22734026db12803ebb3e059622ac3534.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [November 3, 2013, 11:34pm UTC](https://community.unix.com/t/generating-csv-from-column-data/336216/1 "2013-11-03T23:34:44Z")

</div>

Hi List,

I have a chunk of data like so:

```nohighlight
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:

```nohighlight
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 ./

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [November 4, 2013, 1:16am UTC](https://community.unix.com/t/generating-csv-from-column-data/336216/2 "2013-11-04T01:16:07Z")

</div>

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

```nohighlight
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` .

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [November 4, 2013, 5:30am UTC](https://community.unix.com/t/generating-csv-from-column-data/336216/3 "2013-11-04T05:30:39Z")

</div>

You may try this also

```nohighlight
$ 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

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [November 4, 2013, 4:56pm UTC](https://community.unix.com/t/generating-csv-from-column-data/336216/4 "2013-11-04T16:56:56Z")

</div>

try also:

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

```
