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

36 views
Skip to first unread message

Atul Sharma

unread,
Jul 20, 2018, 11:01:47 PM7/20/18
to Ansible Project
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
###########
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
  connection: local


  tasks:

   - name: display multiple file contents
     shell:  echo item
     with_file:
           - gpc.csv
     register: command


   - debug: "var={{item}} | awk -F [,] '{ print $3 }'"
     with_items: command



Please help how to iterate line by line and separate with   comma with awk

Kai Stian Olstad

unread,
Jul 21, 2018, 1:46:24 PM7/21/18
to ansible...@googlegroups.com
On 21.07.2018 05:01, Atul Sharma wrote:
> 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?
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.

- debug: msg="'{{ item.split(',')[2] }}' '{{ item.split(',')[3] }}'"
with_lines: cat gpc.csv


--
Kai Stian Olstad

Atul Sharma

unread,
Jul 21, 2018, 2:46:13 PM7/21/18
to ansible...@googlegroups.com
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:
##########


[root@master workgruop]# ansible-playbook testplay.yml
 [WARNING]: Could not match supplied host pattern, ignoring: all

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [localhost] ***********************************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [localhost]

TASK [Iterating line by line and priniting cooum 3 & 4] ********************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 3\n\nThe error appears to have been in '/root/workgruop/testplay.yml': line 7, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n   - name: Iterating line by line and priniting cooum 3 & 4\n     ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: list object has no element 3"}
        to retry, use: --limit @/root/workgruop/testplay.retry

PLAY RECAP *****************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

[root@master workgruop]#





--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/QFSz-QJb5a8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1f181a75e60287b04d27651b96ba11f1%40olstad.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages