Removing characters from a string via Terminal

My main goal is to be able use Terminal to turn

http://t.umblr.com/redirect?z=https%3A%2F%2Farchiveofourown.org%2Fworks%2F1885509&t=N2M3YmNhNjQzODU5MjI2NmE5ZjljY2Y4NWVhOTVhM2IwYzgyYzYyYixLOHhQakVRbA%3D%3D

into

https://archiveofourown.org/works/1885509

I thought a good place to start would be with finding a way to remove the http://t.umblr.com/redirect?z=.

I triedtr -d; however, there does not seem to be a way to remove only the first instance of a group of characters.

The following (using cut) will only work consistently if there length of the string never changes, which means there is no guarantee for future use.

 echo 'http://t.umblr.com/redirect?z=https%3A%2F%2Farchiveofourown.org%2Fworks%2F1885509&t=N2M3YmNhNjQzODU5MjI2NmE5ZjljY2Y4NWVhOTVhM2IwYzgyYzYyYixLOHhQakVRbA%3D%3D' | cut -c31-83

This looked promising:

 var="http://t.umblr.com/redirect?z=https%3A%2F%2Farchiveofourown.org%2Fworks%2F1885509&t=N2M3YmNhNjQzODU5MjI2NmE5ZjljY2Y4NWVhOTVhM2IwYzgyYzYyYixLOHhQakVRbA%3D%3D" | var=${var:30} 

But because I can not get | pbcopy to work, not could I get outer to print, I have idea what it actually does.

I don't know if there is shell script that will allow me to keep what appears between the equal signs. From the tutorials and questions I have found, sed looks like it would be my best option (if I can figure it out) but it seems to work for files, not strings.

Where is a better place for me to start?


Solution 1:

This seems to work:

$ echo 'http://t.umblr.com/redirect?z=https%3A%2F%2Farchiveofourown.org%2Fworks%2F1885509&t=N2M3YmNhNjQzODU5MjI2NmE5ZjljY2Y4NWVhOTVhM2IwYzgyYzYyYixLOHhQakVRbA%3D%3D' | awk 'BEGIN{FS="=|&"}{gsub(/%3A/, ":")}{gsub(/%2F/, "/")}{print $2}'
https://archiveofourown.org/works/1885509
$

What happening here is the URL string is placed in single quotes and using echo is piped | into an awk program.

Example Syntax:

echo 'URL' | awk 'BEGIN{FS="=|&"}{gsub(/%3A/, ":")}{gsub(/%2F/, "/")}{print $2}'

Solution 2:

Using Perl, create this text file and name it something like "redo.pl":

my $url = $ARGV[0];
$url =~ s/%3A/:/g;
$url =~ s/%2F/\//g;
$url =~ /=\K(.*)(?=&)/;
print $1;

It can be run from a terminal as:

 perl redo.pl http://t.umblr.com...

The output will be:

https://archiveofourown.org/works/1885509

Solution 3:

If you want to use sed:

echo 'URL' | cut -d = -f 2 | sed -e 's|\%3A|:|g' -e 's|\%2F|/|g'

or even without cut and just one call to sed

echo 'URL' | sed -e 's|.*z=\(.*\)\&.*|\1|' -e 's|\%3A|:|g' -e 's|\%2F|/|g'