# how to iterate line by line from csv file and use awk command to separate the comma , field

**URL:** <https://forum.ansible.com/t/how-to-iterate-line-by-line-from-csv-file-and-use-awk-command-to-separate-the-comma-field/28112>\
**Category:** Ansible Project\
**Created:** [July 21, 2018, 3:01am UTC](https://forum.ansible.com/t/how-to-iterate-line-by-line-from-csv-file-and-use-awk-command-to-separate-the-comma-field/28112 "2018-07-21T03:01:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Atul\_Sharma](https://forum.ansible.com/u/Atul_Sharma)\
**Post date:** [July 21, 2018, 3:01am UTC](https://forum.ansible.com/t/how-to-iterate-line-by-line-from-csv-file-and-use-awk-command-to-separate-the-comma-field/28112/1 "2018-07-21T03:01:47Z")

</div>

i want to loop in file to read line by line and in same line seperate the comma, with awk for example

i have file called gpc.csv which contain comma separated value , I want to read leach line and separate feild 3 and 4

gpc.csv file

---

<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:** [July 21, 2018, 5:46pm UTC](https://forum.ansible.com/t/how-to-iterate-line-by-line-from-csv-file-and-use-awk-command-to-separate-the-comma-field/28112/2 "2018-07-21T17:46:15Z")

</div>

> i want to loop in file to read line by line and in same line seperate the  
> comma, with awk for example
> 
> i have file called gpc.csv which contain comma separated value , I want to  
> read leach line and separate feild 3 and 4

I'm not sure if I understand you correctly, but you are trying to print out column 3 and 4?

> gpc.csv file  
> ###########  
> Production,Run PoverA script for  
> Owner,pbst\_o1,/appl/pbst/pbst\_o1/bin/povera\_bst.ksh,20  
> Production,Run PoverA script for  
> Batch,pbst\_b1,/appl/pbst/pbst\_o1/bin/povera\_bst.ksh,30  
> Acceptance,Backup .passfile file,abst\_o,cp  
> /appl/abst/abst\_o1/etc/bst/.passfile /tmp,5  
> Acceptance,Clearing BATCH files from var/log and var/tmp folder, abst\_b,  
> "rm -rf /appl/abst/abst\_o1/var/log/\* /appl/abst/abst\_o1/var/tmp/\*;\  
> find /appl/abst/abst\_o1/var/log/ -user abst\_b1 -type f -exec rm {} ;\  
> find /appl/abst/abst\_o1/var/log/ -user abst\_b1 -type l -exec rm {} ;\  
> find /appl/abst/abst\_o1/var/tmp/ -user abst\_b1 -type f -exec rm {} ;\  
> find /appl/abst/abst\_o1/var/tmp/ -user abst\_b1 -type l -exec rm {} ;\  
> find /appl/abst/abst\_o1/var/log/ ! -name log -user abst\_b1 -type d -exec  
> rmdir {} ;\  
> find /appl/abst/abst\_o1/var/tmp/ ! -name tmp -user abst\_b1 -type d -exec  
> rmdir {} \,5"  
> Acceptance,Clearing OWNER files from var/log and var/tmp  
> folder,abst\_o,"find /appl/abst/abst\_o1/var/log/ -user abst\_o1 -t\ype f  
> -exec rm {} \;\  
> find /appl/abst/abst\_o1/var/tmp/ -user abst\_o1 -type f -exec rm {} \;  
> find /appl/abst/abst\_o1/var/log/ ! -name log -user abst\_o1 -type d -exec  
> rmdir {} \;  
> find /appl/abst/abst\_o1/var/tmp/ ! -name tmp -user abst\_o1 -type d -exec  
> rmdir {} \;  
> ls -1a /appl/abst/abst\_o1| grep -Evw "".|..|var|.ssh"" | xargs -I {} rm -rf  
> {},5"  
> Acceptance,Creating tmp folder,abst\_o mkdir -m 2775 /appl/abst/abst\_o1/tmp,5
> 
> playbook i am using but it is not working as i want:  
> ###########################################
> 
> ---  
> - hosts: localhost  
> &nbsp;&nbsp;connection: local
> 
> &nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;&nbsp;- name: display multiple file contents  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: echo item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_file:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- gpc.csv  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: command
> 
> &nbsp;&nbsp;&nbsp;- debug: "var={{item}} | awk -F [,] '{ print $3 }'"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: command

You can't use awk in Ansible. That is a shell command and can only be used in modules shell and command.

But you could do something like this.

&nbsp;&nbsp;&nbsp;- debug: msg="'{{ item.split(',')[2] }}' '{{ item.split(',')[3] }}'"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_lines: cat gpc.csv

---

<div class="post-metadata">

**Author:** ![Atul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Atul\_Sharma](https://forum.ansible.com/u/Atul_Sharma)\
**Post date:** [July 21, 2018, 6:46pm UTC](https://forum.ansible.com/t/how-to-iterate-line-by-line-from-csv-file-and-use-awk-command-to-separate-the-comma-field/28112/3 "2018-07-21T18:46:04Z")

</div>

Yes you understood correctly I want to run loop over gpc.csv file and print 3 and 4 ,

i tried but spiting with index number is not working , Following error is throwing

Following playbook using as suggested:

tasks:

- name: Iterating line by line and priniting cooum 3 & 4  
debug: msg=" ‘{{ item.split(’,‘)[3] }}’ ‘{{ item.split(’,‘)[4] }}’ "  
with\_lines: cat gpc.csv

error:
