Browse Source

findinpkg: partial rewrite

master
Pekka Helenius 4 years ago
parent
commit
0d6fdcdcfe
1 changed files with 32 additions and 17 deletions
  1. +32
    -17
      tools/findinpkg.sh

+ 32
- 17
tools/findinpkg.sh View File

@ -18,34 +18,49 @@ if [[ ! $(pacman -Qq "${1}" 2> /dev/null) ]]; then
help help
fi fi
files=()
filename=""
lines=()
filematches=()
lines_total=0 lines_total=0
files_total=0 files_total=0
typeset -A FILEMATCHES
esc=$(printf '\033') esc=$(printf '\033')
for file in $(pacman -Ql "${1}" | sed "s/^${1}\s//g"); do for file in $(pacman -Ql "${1}" | sed "s/^${1}\s//g"); do
if [[ -f "${file}" ]]; then if [[ -f "${file}" ]]; then
line=$(grep --line-number --with-filename --binary-files=without-match -i "${2}" "${file}" | sed -r "s/^(.*?:)([0-9]+):.*/\1\2/g")
filestr=$(grep --line-number --with-filename --binary-files=without-match -i "${2}" "${file}" | sed -r "s/^(.*?:)([0-9]+):.*/\1\2/g")
for l in ${line[@]}; do
# In some rare cases grepped files are presented on the same line
for filestr in ${filestr[@]}; do
filematches+=(${filestr})
done
fi
done
linenum=$(printf '%s' ${l} | sed -r 's/^.*?:([0-9]+$)/\1/')
filename_=$(printf '%s' ${l} | sed -r 's/^(.*?):[0-9]+$/\1/')
for filematch in ${filematches[@]}; do
filename_=$(printf '%s' "${filematch}" | sed -r 's/^(.*?):[0-9]+$/\1/')
lines+=(${linenum})
if [[ ${FILEMATCHES[$filename_]} == "" ]]; then
lines=()
echo "Processing file: ${filename_}"
if [[ ! "${filename}" == "${filename_}" ]]; then
echo "${esc}[35m${filename_}${esc}[92m: ${esc}[31m${lines[*]}${esc}[0m"
lines_total=$(( ${lines_total} + ${#lines[@]} ))
let files_total++
lines=()
fi
# Get all occurences
singlefile=$(echo ${filematches[@]} | grep -oE "(${filename_}:[0-9]+)" )
filename="${filename_}"
done
fi
for occurence in ${singlefile[@]}; do
linenum=$(printf '%s' "${occurence}" | sed -r 's/^.*?:([0-9]+$)/\1/')
lines+=(${linenum})
done
FILEMATCHES+=([$filename_]="${lines[*]}")
lines_total=$(( ${lines_total} + ${#lines[@]} ))
let files_total++
fi
done done
for filename in ${!FILEMATCHES[@]}; do
echo "${esc}[35m${filename}${esc}[92m: ${esc}[31m${FILEMATCHES[$filename]}${esc}[0m"
done
printf "\nFound %d matching lines for pattern '%s' in %d files in package '%s'.\n\n" ${lines_total} "${2}" ${files_total} "${1}" printf "\nFound %d matching lines for pattern '%s' in %d files in package '%s'.\n\n" ${lines_total} "${2}" ${files_total} "${1}"

Loading…
Cancel
Save