The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

20 lines
782 B

  1. #!/bin/bash
  2. if [[ "$1" == '--help' || "$1" == '-h' ]]
  3. then
  4. echo "Usage: parent-urls [--with-original]" >&2
  5. echo "Take URLs on stdin, remove path components one by one and print the resulting URLs on stdout." >&2
  6. echo "If --with-original is given, the input URL is printed before the parents." >&2
  7. fi
  8. prog=''
  9. prog+='BEGIN { FS="/"; OFS="/" }' # Set the field separators
  10. if [[ "$1" == '--with-original' ]]
  11. then
  12. prog+='{ print }' # Print the input URL (if desired)
  13. fi
  14. prog+='/[?#]/ { sub(/[?#].*/, "") }' # Remove query and fragment if present
  15. prog+='/\/$/ { --NF }' # Remove the trailing blank field if the URL ends with a slash
  16. prog+='{ for (i = NF; i > 3; --i) { --NF; print $0 OFS; } }' # One by one, remove the last path component, print remaining fields
  17. exec awk "${prog}"