1

I'm doing some queries on my Drupal site using the drush sql-query command. I'd like to export the results to a CSV. Is it possible to do this via the command options, some plugin, or some other alternative method?

1

The awk part should be like this

drush sqlq "SELECT nid, title, alias from node 
LEFT JOIN url_alias ON url_alias.source = CONCAT('node/', node.nid)
LEFT JOIN field_data_field_myfield ON node.nid = field_data_field_myfield.entity_id 
WHERE type = 'blog' AND field_data_field_myfield.entity_id IS NULL;"| awk 'BEGIN { FS = "\t"} ; {print "\""$1"\",\""$2"\",\""$3"\""}' > output.csv
  • The awk command doesn't work. The output is all columns of "$1,$2,$3" – Brian Young Oct 17 '16 at 1:25
  • For a comma separated list, the awk part has to be like: awk 'BEGIN { FS = "\t"} ; {print "\""$1"\","\"$2"\","\"$3"\""}' > output.csv. Note the backslashes which will escape the second double-quote so it will appear as an actual double-quote inside the csv. – cirrus3d Oct 4 '17 at 11:32
1

I found it easiest to export the results to .tsv format

  • D7: drush sql-query "SELECT nid, title FROM node LIMIT 10" > nid-title.tsv
  • D8: drush sql-query "SELECT nid, title FROM node_field_data LIMIT 10" > nid-title.tsv

You could also convert TSV to CSV from the linux terminal with something like:

cat nid-title.tsv | tr "\\t" "," > nid-title.csv

or with an editor like Libre Offic Calc: open .tsv, save as .csv

  • If tr is not installed use sed: cat nid-title.tsv | sed "s/\t/,/g" > nid-title.csv. – alec Feb 16 at 1:33

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.