Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

up vote 6 down vote accepted

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
share|improve this answer
    
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

Besides sed, there is awk...

awk -F, 'NR > 1 { system("./mycommand --name=\\\"" $1 "\\\" --age=" $2 " --address=\\\"" $3 "\\\"") }' < file.csv
share|improve this answer
    
Love the simplicity of this one. Thanks!! Will test ASAP. –  MZAweb Jun 26 '12 at 1:18

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.

share|improve this answer
    
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 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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