From 4eff3c3eb3d121bd2d78852129eb2e20ea553479 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Fri, 15 Jan 2021 17:23:46 +0000 Subject: [PATCH] Refactor, strip query/fragment --- parent-urls | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/parent-urls b/parent-urls index 85b89c0..fea7e64 100755 --- a/parent-urls +++ b/parent-urls @@ -6,16 +6,14 @@ then echo "If --with-original is given, the input URL is printed before the parents." >&2 fi -prog0='BEGIN { FS="/"; OFS="/" }' # Set the field separators -prog1='{ print }' # Print the input URL (if desired) -prog2='/\/$/ { --NF }' # Remove the trailing blank field if the URL ends with a slash -prog3='{ for (i = NF; i > 3; --i) { --NF; print $0 OFS; } }' # One by one, remove the last path component, print remaining fields - -prog="${prog0} " +prog='' +prog+='BEGIN { FS="/"; OFS="/" }' # Set the field separators if [[ "$1" == '--with-original' ]] then - prog="${prog}${prog1} " + prog+='{ print }' # Print the input URL (if desired) fi -prog="${prog}${prog2} ${prog3}" +prog+='/[?#]/ { sub(/[?#].*/, "") }' # Remove query and fragment if present +prog+='/\/$/ { --NF }' # Remove the trailing blank field if the URL ends with a slash +prog+='{ for (i = NF; i > 3; --i) { --NF; print $0 OFS; } }' # One by one, remove the last path component, print remaining fields exec awk "${prog}"