Skip to content

Instantly share code, notes, and snippets.

@KR1470R
Last active May 7, 2022 14:22
Show Gist options
  • Save KR1470R/4d256eb498bc597aa93a89602af68919 to your computer and use it in GitHub Desktop.
Save KR1470R/4d256eb498bc597aa93a89602af68919 to your computer and use it in GitHub Desktop.
This script actually used for multiline replacing href in ~/.local/share/okular/bookmarks.xml
#!/usr/bin/env sh
echo ${1:?Destination path is not specified}
export lines=$(cat $1 | egrep '<folder href|<bookmark href')
export IFS=$(printf '>')
# sed -i 's,link,new-link,g' $1
function main () {
for line in $lines; do
# echo "Current line>"$line
if test $(echo $line | grep -o "DELETED|" | wc -m) -ne 0; then
continue
elif test $(echo $line | grep -o "Education" | wc -m) -eq 0; then
export clear_href=$(tr -d '\n' <<< $(echo $line | cut -d '"' -f 2))
export clear_href_decoded=$(echo $clear_href | sed -e "s/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g" | xargs -0 echo -e)
export href=$(echo $clear_href | cut -f1 -d"#")
printf 'Change for: %s'$clear_href_decoded
echo
if test $(echo $href | wc -m) -ge 2; then
read new_href
if [[ $new_href == "del" ]]; then
echo "Marking $href as DELETED"
export replace_string="DELETED|"$href
sed -i "s,$href,$replace_string,g" $1
export lines=$(cat $1 | egrep '<folder href|<bookmark href')
main $1
else
if test $(echo $new_href | wc -m) -le 1; then
continue
else
#export href_esc=$(sed 's/\%/\\%/g' <<< $href)
echo "OLD HREF: "$href
echo "NEW HREF: "$new_href
sed -i "s|$href|$new_href|g" $1
export lines=$(cat $1 | egrep '<folder href|<bookmark href')
main $1
fi
fi
else
echo "Href is empty. Passing..."
continue
fi
else
continue
fi
done
}
main $1
@KR1470R
Copy link
Author

KR1470R commented May 7, 2022

Destination path is ~/.local/share/okular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment