0

I have some data that looks like this "thumbnailUrl": "http://placehold.it/150/adf4e1"

I want to know how I can get the trailing part of the URL, I want the output to be adf4e1

I was trying to grep when starting with / and ending with " but I'm only a beginner in shell scripting and need some help.

CuriousDog
  • 35
  • 8
  • Perhaps more importantly, how are you getting that string in the first place? It looks like a JSON fragment; a tool like `jq` may be more suitable for getting the value you want directly from the original. – chepner Sep 30 '16 at 18:14

5 Answers5

1

I came up with a quick and dirty solution, using grep (with perl regex) and cut:

$ cat file
  "thumbnailUrl": "http://placehold.it/150/adf4e1"
  "anotherUrl": "http://stackoverflow.com/questions/3979680"
  "thumbnailUrl": "http://facebook.com/12f"
  "randortag": "http://google.com/this/is/how/we/roll/3fk19as1"

$ cat file | grep -o '/\w*"$' | cut -d'/' -f2- | cut -d'"' -f1
  adf4e1
  3979680
  12f
  3fk19as1
galfisher
  • 1,122
  • 1
  • 13
  • 24
1

We could kill this with a thousand little cuts, or just one blow from Awk:

awk -F'[/"]' '{ print $(NF-1); }'

Test:

$  echo '"thumbnailUrl": "http://placehold.it/150/adf4e1"' \
     | awk -F'[/"]' '{ print $(NF-1); }'
adf4e1

Filter thorugh Awk using double quotes and slashes as field separators. This means that the trailing part ../adf4e1" is separated as {..}</>{adf4e1}<">{} where curly braces denote fields and angle brackets separators. The Awk variable NF gives the 1-based number of fields and so $NF is the last field. That's not the one we want, because it is blank; we want $(NF-1): the second last field.

"Golfed" version:

awk -F[/\"] '$0=$(NF-1)'
Kaz
  • 55,781
  • 9
  • 100
  • 149
1

If the original string is coming from a larger JSON object, use something like jq to extract the value you want.

For example:

$ jq -n '{thumbnail: "http://placehold.it/150/adf4e1"}' |
> jq -r '.thumbnail|split("/")[-1]'
adf4e1

(The first command just generates a valid JSON object representing the original source of your data; the second command parses it and extracts the desired value. The split function splits the URL into an array, from which you only care about the last element.)

chepner
  • 497,756
  • 71
  • 530
  • 681
1

You can also do this purely in bash using string replacement and substring removal if you wrap your string in single quotes and assign it to a variable.

#!/bin/bash

string='"thumbnailUrl": "http://placehold.it/150/adf4e1"'
string="${string//\"}"
echo "${string##*/}"

adf4e1 #output
I0_ol
  • 1,054
  • 1
  • 14
  • 28
0

You can do that using 'cut' command in linux. Cut it using '/' and keep the last cut. Try it, its fun!

Refer http://www.thegeekstuff.com/2013/06/cut-command-examples

dganesh2002
  • 1,917
  • 1
  • 26
  • 29
  • How can you use `cut` to get the last parameter? – galfisher Sep 30 '16 at 18:11
  • @galfisher few options http://stackoverflow.com/questions/22727107/how-to-find-the-last-field-using-cut-linux – dganesh2002 Sep 30 '16 at 18:17
  • Alternative is - awk -F / '{print $NF}' – dganesh2002 Sep 30 '16 at 18:19
  • @galfisher `cut -f5 -d/` -- but the `awk` solution is better, as it's more forgiving about multiple delimiters in a row and thus counting field numbers, esp. when the delimiter is space. – gilez Sep 30 '16 at 19:49
  • @gilez this `cut` solution only works if the url is exactly of http://.../../... - that's the weakness of `cut` - can't get last column unless you know the exact number of columns (or delimiters), i agree that `awk` is a good solution for this problem. – galfisher Sep 30 '16 at 20:08