# Converting variable space width data into CSV data in bash

**URL:** <https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031>\
**Category:** Shell Programming and Scripting\
**Created:** [April 25, 2012, 3:28am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031 "2012-04-25T03:28:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vharsha](https://community.unix.com/letter_avatar/vharsha/32/5_5575768a8748004e209b776fc1b2916d.png) [@vharsha](https://community.unix.com/u/vharsha)\
**Post date:** [April 25, 2012, 3:28am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031/1 "2012-04-25T03:28:14Z")

</div>

Hi All,

I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for.

My Input data in inputfile.txt

```nohighlight
   19 15657 15685 Sr2dReader 107.88 105.51 10404 RUNNING

```

My output should be

```nohighlight
19,15657,15685,Sr2dReader,107.88,105.51,10404,RUNNING

```

Thanks a many for your inputs.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 25, 2012, 3:32am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031/2 "2012-04-25T03:32:38Z")

</div>

Try

```nohighlight
awk '{$1=$1}1' OFS=, infile

```

---

<div class="post-metadata">

**Author:** ![sagar\_1986](https://community.unix.com/user_avatar/community.unix.com/sagar_1986/32/2605_2.png) [@sagar\_1986](https://community.unix.com/u/sagar_1986)\
**Post date:** [April 25, 2012, 3:39am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031/3 "2012-04-25T03:39:09Z")

</div>

try this

```nohighlight
csv=`cat filename |tr -s " " ","`

```

---

<div class="post-metadata">

**Author:** ![vharsha](https://community.unix.com/letter_avatar/vharsha/32/5_5575768a8748004e209b776fc1b2916d.png) [@vharsha](https://community.unix.com/u/vharsha)\
**Post date:** [April 25, 2012, 3:43am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031/4 "2012-04-25T03:43:57Z")

</div>

works beautifully. Thanks a lot.

---------- Post updated at 12:43 AM ---------- Previous update was at 12:42 AM ----------

thank u.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [April 25, 2012, 4:41am UTC](https://community.unix.com/t/converting-variable-space-width-data-into-csv-data-in-bash/309031/5 "2012-04-25T04:41:07Z")

</div>

> [@sagar\_1986](#):
>
> try this
> 
> csv=`cat filename |tr -s " " ","`

useless use of cat award

[Useless Use of Cat Award](http://www.smallo.ruhr.de/award.html)

```plaintext
$ tr -s " " "," < input.txt

```
