# Writing to csv file

**URL:** <https://forum.ansible.com/t/writing-to-csv-file/30444>\
**Category:** Ansible Project\
**Created:** [May 29, 2019, 2:45pm UTC](https://forum.ansible.com/t/writing-to-csv-file/30444 "2019-05-29T14:45:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asha\_Omanakuttan](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Asha\_Omanakuttan](https://forum.ansible.com/u/Asha_Omanakuttan)\
**Post date:** [May 29, 2019, 2:45pm UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/1 "2019-05-29T14:45:00Z")

</div>

I need some contents to be written in csv format to a file.

shell: ps -ef | grep java | awk '{print $1}'  
register: username

shell: ps -ef | grep java | awk '{print $2}'  
register: id

The output for username will be similar to :  
root  
admin  
test\_admin

The output for id will be similar to:  
1232  
4343  
2233

so I want this to be written to a csv file as

root,1232  
admin,4343  
test\_admin,2233

Please suggest.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 29, 2019, 3:02pm UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/2 "2019-05-29T15:02:18Z")

</div>

Hello Asha,

why not one task which already gives you the desired output:

ps -ef | grep java | awk '{print $1","$2}'

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 29, 2019, 3:07pm UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/3 "2019-05-29T15:07:59Z")

</div>

Since you already using shell, do everything in one task

- shell: ps -ef | awk '/java/ { print $1","$2 }' \> /path/my.csv

---

<div class="post-metadata">

**Author:** ![Asha\_Omanakuttan](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Asha\_Omanakuttan](https://forum.ansible.com/u/Asha_Omanakuttan)\
**Post date:** [May 30, 2019, 9:49am UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/4 "2019-05-30T09:49:30Z")

</div>

Actually I gave those as a sample..both commands will be different.. requirement is to merge both.

---

<div class="post-metadata">

**Author:** ![Asha\_Omanakuttan](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Asha\_Omanakuttan](https://forum.ansible.com/u/Asha_Omanakuttan)\
**Post date:** [May 30, 2019, 9:57am UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/5 "2019-05-30T09:57:45Z")

</div>

Both are different commands. If I use shell and \>\>test.csv for each task, it gets appended as  
Root  
Admin  
Test\_admin  
1232  
4343  
2233.  
But I need it in other way.

For eg  
Shell: ps -ef | grep Java | awk '{print $1}'  
Shell: ps -ef | grep Java | grep app | awk '{print $2}'

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [May 30, 2019, 1:12pm UTC](https://forum.ansible.com/t/writing-to-csv-file/30444/6 "2019-05-30T13:12:57Z")

</div>

If you are certain that your two lists will be the same size, then you can try using an index variable. See

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#tracking-progress-through-a-loop-with-index-var](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#tracking-progress-through-a-loop-with-index-var)

Iterate through one list, declare an index variable, and use the index variable to get the appropriate member out of the second list.

–EbH
