Using CSV line as command parameters

7,984

Solution 1

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

Solution 2

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.

Solution 3

Besides sed, there is awk...

awk -F, 'NR > 1 { system("./mycommand --name=\\\"" $1 "\\\" --age=" $2 " --address=\\\"" $3 "\\\"") }' < file.csv
Share:
7,984

Related videos on Youtube

MZAweb
Author by

MZAweb

Updated on September 18, 2022

Comments

  • MZAweb
    MZAweb over 1 year

    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?

    • Bernhard
      Bernhard almost 12 years
      Do you want name, age and address also from the CSV file?
  • MZAweb
    MZAweb almost 12 years
    Is it possible that you're missing a close quote?
  • rush
    rush almost 12 years
    Oh, sorry. I did. But right now it is fixed. Thank you.
  • MZAweb
    MZAweb almost 12 years
    How are you passing the file contents to this?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 12 years
    @MZAweb Oh, yeah. Redirect the input of that shell snippet from the file or pipe containing your data. See my edit.
  • MZAweb
    MZAweb almost 12 years
    Love the simplicity of this one. Thanks!! Will test ASAP.
  • Simon Gates
    Simon Gates almost 12 years
    +1 for the ‘simple CSV’ proviso. CSV is much harder to parse from the shell than its acronym would have you believe.
  • JaakL
    JaakL almost 7 years
    I like this one much more than the sed magic in accepted answer, as it is actually readable (i.e. usable).
  • Vasily802
    Vasily802 over 6 years
    could you please explain what \\\ mean