Skip to content

Instantly share code, notes, and snippets.

@liuyix
Created November 28, 2013 03:49
Show Gist options
  • Select an option

  • Save liuyix/7687011 to your computer and use it in GitHub Desktop.

Select an option

Save liuyix/7687011 to your computer and use it in GitHub Desktop.

http://stackoverflow.com/a/5395421

Here's a fairly gnarly way of doing it. Found it somewhere, can't take any credit

mysql --user=wibble --password wobble -B -e "select * from vehicle_categories;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > vehicle_categories.csv

Works pretty well. Once again though a regex proves write only.

Regex Explanation:

s/// means substitute what's between the first // with what's between the second //
the "g" at the end is a modifier that means "all instance, not just first"
^ (in this context) means beginning of line
$ (in this context) means end of line

So, putting it all together:

s/'/\'/          replace ' with \'
s/\t/\",\"/g     replace all \t (tab) with ","
s/^/\"/          at the beginning of the line place a "
s/$/\"/          at the end of the line place a "
s/\n//g          replace all \n (newline) with nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment