Skip to content

Instantly share code, notes, and snippets.

@tsjk
Created March 10, 2016 08:49
Show Gist options
  • Save tsjk/3aff3382746dd9c59ace to your computer and use it in GitHub Desktop.
Save tsjk/3aff3382746dd9c59ace to your computer and use it in GitHub Desktop.
Search for nodes in an XML containing an expression and report the xpaths of the findings, using xml-coreutils and coreutils
#!/bin/bash
SEARCH_STRING="${1}"
ROOT_NODE="${2}"
FILE="${3}"
XPATHS=( $(xml-grep --extended-regexp --attributes "${SEARCH_STRING}" "${FILE}" | xml-fmt | xml-find | sed 's@^/root@/'"${ROOT_NODE}"'@' | tac) )
i_="";
NODE_XPATHS=( $(for i in "${XPATHS[@]}"; do if ! echo "${i_}" | egrep -q "${i}"; then echo "${i}"; fi; i_="${i}"; done) )
for i in "${NODE_XPATHS[@]}"; do
k=1;
for j in $(xml-printf '%s' "${FILE}" ":${i}"); do
if echo "${j}" | egrep -q -E "${SEARCH_STRING}"; then
echo "${i}[${k}]: ${j}"
fi
k=$[${k}+1]
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment