trevmex's tumblings

JavaScripter, Rubyist, Functional Programmer, Agile Practitioner.

Rails one-liner to get URLs from rake routes

Earlier today I needed to get a list of URL paths that my Rails app provided, but I didn’t need all the extra cruft that you get with

rake routes

So I created a bash one-liner with sed, awk, uniq, and sort to get what I needed out of rake routes. The one liner looks like:

rake routes | sed -e "1d" -e "s,^[^/]*,,g" | awk '{print $1}' | sort | uniq 

Let’s look at what this is doing:

  1. First we get the list of routes:
    rake routes
  2. Next we pipe the result to sed, which does two things:
    1. Deletes the first line of the output:
      sed -e "1d"
    2. Then it removes every character from the beginning of each line (^) that is not a forward-slash ([^/]*) and removes it:
      -e "s,^[^/]*,,g"
  3. We then pipe that result to awk which prints out only the first column of the output:
    awk '{print $1}'
  4. Next we:
    sort
    the result.
  5. And finally we remove duplicates with:
    uniq

Now we can copy this to an email to the client, and all we have to do is explain what the :id and (.:format) are, as opposed to sending them the raw output of rake routes with all that extra information.

Enjoy!