Many times we may need to convert normal text files to comma delimited or any delimited forms. When dealing with Ansible, a comma-delimited form is very useful. Excel we can use but on Linux console, we can use follow two-liner to do work for us.
Bash Shell script to convert text file to delimited CSV format. Consider I have 300 hosts to gather some ifup -a o/p using Ansible playbook. I have an inventory of 2000 hosts with my Ansible. so I can pass selected hosts as variable to Ansible playbook in comma-delimited form. How to do it in Linux Shell in a quick way?
[root@localhost work]# cat big.sh
#!/bin/sh
sv_string=$(paste -sd, hosts)
echo $sv_string
[root@localhost work]#
Code language: PHP (php)
usage:
[root@localhost work]# ./big.sh
abc.example.com,bca.com,zfa.com,zyz.com,manyhostslikethis.com,
[root@localhost work]# cat hosts
abc.example.com
bca.com
zfa.com
zyz.com
manyhostslikethis.com
Code language: PHP (php)