Shell – Using CSV line as command parameters

csvpipeshelltext processing

I have a CSV file like:

Name,Age,Address
Daniel Dvorkin,28,Some Address St. 1234
(... N ...)
Foo Bar,90,Other Address Av. 3210

And I have a command that take this parameters:

./mycommand --name="Daniel Dvorkin" --age=28 --address="Some Address St. 1234"

What is the easiest way to run mycommand for each line of the CSV?

Best Answer

That's pretty easy:

sed '1d;s/\([^,]*\),\([^,]*\),\([^,]*\)/.\/mycommand --name="\1" --age="\2" --address="\3"/e' file.csv

1d will delete caption line. s command will modify the string like in your example e in the end of s command will execute the string. this is GNU extension, so if you don't have GNU sed, you can use xargs instead e:

sed '1d;s/\([^,]*\),\([^,]*\),\([^,]*\)/.\/mycommand --name="\1" --age="\2" --address="\3"/' file.csv | xargs
Related Question