8

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?

2
  • Do you want name, age and address also from the CSV file? – Bernhard Jun 25 '12 at 17:28
  • nop. Just the data. – MZAweb Jun 25 '12 at 17:31
8

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
2
  • Is it possible that you're missing a close quote? – MZAweb Jun 25 '12 at 17:48
  • Oh, sorry. I did. But right now it is fixed. Thank you. – rush Jun 25 '12 at 19:19
6

If your CSV is simple CSV with no quoting mechanism (hence commas cannot appear in a field), you can do the parsing in the shell.

{
  read line  # ignore the header line
  IFS=,
  while read -r name age address; do
    ./mycommand --name="$name" --age="$age" --address="$address"
  done
} <input.csv

If fields can be quoted, you need a real CSV parser. Use Perl, Python, R, Ruby or other languages.

4
  • How are you passing the file contents to this? – MZAweb Jun 26 '12 at 0:20
  • @MZAweb Oh, yeah. Redirect the input of that shell snippet from the file or pipe containing your data. See my edit. – Gilles 'SO- stop being evil' Jun 26 '12 at 0:25
  • +1 for the ‘simple CSV’ proviso. CSV is much harder to parse from the shell than its acronym would have you believe. – Alexios Jun 28 '12 at 19:20
  • I like this one much more than the sed magic in accepted answer, as it is actually readable (i.e. usable). – JaakL May 19 '17 at 7:52
1

Besides sed, there is awk...

awk -F, 'NR > 1 { system("./mycommand --name=\\\"" $1 "\\\" --age=" $2 " --address=\\\"" $3 "\\\"") }' < file.csv
2
  • Love the simplicity of this one. Thanks!! Will test ASAP. – MZAweb Jun 26 '12 at 1:18
  • 1
    could you please explain what \\\ mean – Vasily802 Aug 21 '17 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.