22

I'm trying this:

token=`curl -I --header "X-Auth-User: user@user.com" --header "X-Auth-Key: XXXXXXXXXXXXXXXXXXXXXX" api.server.com | grep -Fi X-Auth-Token | awk -F" " '{ print $2}'`

/usr/bin/wget --accept .jpg,.jpeg -p "https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=$token" -O "image.jpg" || rm "image.jpg"

But my token result is:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6%0D

Instead of:

https://api.server.com/v1/stats/1/graph?callback=jQuery171027000000144289315_1380000003353&mnum=1&res_type=cpu&g_type=6h&graph_type=img&full=1&w=515&h=150&X-Auth-Token=SERVER_018d8100000000001d1b817f7d58a6

How to remove %0D (Carriage return)?

rdshck
  • 592
  • 1
  • 5
  • 12

3 Answers3

40

You can add | tr -d '\r' to your curl pipeline to strip any carriage returns.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 1
    The way I read this, it's the sending server which tacks on a URL-encoded carriage return. So there is no client-side CR to trim. – tripleee Nov 25 '13 at 06:10
  • @tripleee The carriage return is carried from the curl HEAD response into the url, and wget url encodes it. You can see both effects with `curl -I google.com | cat -v` and `wget $'http://google.com/\r'` – that other guy Nov 25 '13 at 06:39
  • More info about TR here: http://pubs.opengroup.org/onlinepubs/009695399/utilities/tr.html – Harkály Gergő Nov 13 '17 at 12:32
2

There is a utility called dos2unix. You may have to install it. or use the translate

tr -d '\r' < input > output

EDIT Found a post that discusses a few options: Remove carriage return in Unix

Community
  • 1
  • 1
crafter
  • 6,246
  • 1
  • 34
  • 46
0

I solved it by piping to sed. I was already using sed in my pipe so it made sense.

cmd | sed 's/\r//g'

Ken Sharp
  • 934
  • 9
  • 22