Earlier this morning I mentioned a Lifehacker post that walked you through the process of turning a song list on Wikipedia into a Spotify playlist. The problem with the post was that it used Excel. Some of the comments mentioned using a text editor & find & replace instead, which in my mind is a better solution, but I wanted something more UNIX-y, something that used bash to do the job.

So, after a bit of playing around, here is a bash one-liner that does the job. It assumes that you’ve copied the Wikipedia song list into a text file named 1975.txt1; obviously, adjust for your file’s name:

cut -f 2,3 1975.txt | tr -d '"' | awk 'BEGIN {FS = "\t"} ; {printf("%s \t %s\n", $2, $1);}' | awk 'NR==1{print "Artist\tTitle"}1' > 1975_playlist.txt

Let’s go through this one command at a time.

  • cut -f 2,3 1975.txt: Cut out the second & third columns of the file, thus removing the first column, which had numbers in it.
  • tr -d '"': The tr command (tr for translate characters) deletes the quotation marks that Wikipedia has around each song title.
  • awk 'BEGIN {FS = "\t"} ; {printf("%s \t %s\n", $2, $1);}': I tell the über-powerful awk command that the file is using tab separators, then I switch the columns from Title/Artist to Artist/Title.
  • awk 'NR==1{print "Artist\tTitle"}1': Now awk is used to insert a header row at the top of the file labeling the columns as Artist & Title.
  • Finally, I output the results to a new file: 1975_playlist.txt

The only kinda sucky part of this whole Wikipedia-to-Spotify process is at the end: you have to submit the final file to a website called SpotMySongs, which converts it into a Spotify playlist. I wish there was a way to do that on the command line too, but I don’t know of any. I’ll see if anyone has written anything & get back to you if I find anything.

Finally, bash continues to amaze me at its power & flexibility. Pipes truly are a wondrously helpful tool. The fact that I can link together four commands boom boom boom & output the final results to a file gives me a power greater than that of Thor’s hammer. At least it feels that way. :)

  1. Why 1975? Because it was a good year for music, although there was a lot of dreck too, but that’s true of every year, no?