Sriharsha Kalluru
Sriharsha Kalluru

Reputation: 1823

Read a CSV file in bash

I have a requirement to read the CSV file in shell, Well I am ok with the CSV file having single line in a cell. But if we have multiple lines in cell of CSV file then I am unable to delimit the the CSV file.

Filename            Lines
/etc/hosts          example.test.com
                    example2.test.com
/etc/resolv.conf    nameserver dns.test.com
                    search test.com

I will take input from the user in a CSV file and have to add the given lines to the mentioned files. Here there are multiple lines in each cell of a CSV file and If I try to cat the file it is giving in a different order.

[user2@mon ~]$ cat test2.csv
"Filename","Lines"
"/etc/hosts","example.test.com"
,"example2.test.com"
"/etc/resolv.conf","nameserver dns.test.com"
,"search test.com"

Is there any way we can read the multiple lines from that file and number of lines is not same in all the time.

Upvotes: 2

Views: 7390

Answers (2)

William Pursell
William Pursell

Reputation: 212148

Assuming your input is as basic as your example, you might be able to get away with simply doing:

sed 's/^,/ ,/' test2.csv | tr -d \" | column -s, -t

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

This might be what you're after:

awk -F, '{ sub(/^"/, "", $1); sub(/"$/, "", $1);
           sub(/^"/, "", $2); sub(/"$/, "", $2);
           printf "%-20s  %s\n", $1, $2;
         }'

It may well be possible to compress the substitute operations if you spend more time manual bashing. This is fragile as a solution (most solutions not using code specialized for dealing with CSV format are fragile); it fails horribly if a comma appears inside any of the quote-enclosed fields.

Applied to your data, it yields:

Filename              Lines
/etc/hosts            example.test.com
                      example2.test.com
/etc/resolv.conf      nameserver dns.test.com
                      search test.com

Other possible tools to manipulate CSV format data reliably include:

If this is not what you are looking for, please clarify the question.

Upvotes: 2

Related Questions