Terminal Command Line – Removing Characters from a String via Terminal

command lineterminal

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?

Best Answer

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}'