Drupal 11: Export An SQL Query As CSV Using Drush

To run an SQL query with Drush we need to use the sql:query command. By combining this with awk we can render the output as a CSV and pipe it into a file.

./vendor/bin/drush sql:query "select *
from node_field_data
where type = 'page'
;" | awk 'BEGIN { FS = "\t"} ; {print "\""$1"\",\""$2"\",\""$3"\",\""$4"\",\""$5"\",\""$6"\",\""$7"\",\""$8"\",\""$9"\",\""$10"\",\""$11"\",\""$12"\",\""$13"\",\""$14"\""}' > somefile.csv

The file at somefile.csv is a CSV export of our query.

Add new comment

The content of this field is kept private and will not be shown publicly.