Useful CLI tools (bash) for Arch Linux administration
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.

81 lines
2.4 KiB

  1. #!/usr/bin/env bash
  2. #
  3. # findinpkg - Find text patterns & print occurences with matching lines numbers in Arch Linux package files
  4. #
  5. # Copyright (C) 2021 Pekka Helenius <pekka.helenius@fjordtek.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #####################################
  20. function help() {
  21. echo -e "Usage: findinpkg <pkgname> <pattern-to-search>"
  22. exit
  23. }
  24. if [[ ${#@} -ne 2 ]]; then
  25. help
  26. fi
  27. if [[ ! $(pacman -Qq "${1}" 2> /dev/null) ]]; then
  28. echo "Can't find package ${1}"
  29. help
  30. fi
  31. filematches=()
  32. lines_total=0
  33. files_total=0
  34. typeset -A FILEMATCHES
  35. esc=$(printf '\033')
  36. for file in $(pacman -Qlq "${1}"); do
  37. if [[ -f "${file}" ]]; then
  38. filestr=$(grep --line-number --with-filename --binary-files=without-match -i "${2}" "${file}" | sed -r "s/^(.*?:)([0-9]+):.*/\1\2/g")
  39. # In some rare cases grepped files are presented on the same line
  40. for filestr in ${filestr[@]}; do
  41. filematches+=(${filestr})
  42. done
  43. fi
  44. done
  45. for filematch in ${filematches[@]}; do
  46. filename_=$(printf '%s' "${filematch}" | sed -r 's/^(.*?):[0-9]+$/\1/')
  47. if [[ ${FILEMATCHES[$filename_]} == "" ]]; then
  48. lines=()
  49. echo "Processing file: ${filename_}"
  50. # Get all occurences
  51. singlefile=$(echo ${filematches[@]} | grep -oE "(${filename_}:[0-9]+)" )
  52. for occurence in ${singlefile[@]}; do
  53. linenum=$(printf '%s' "${occurence}" | sed -r 's/^.*?:([0-9]+$)/\1/')
  54. lines+=(${linenum})
  55. done
  56. FILEMATCHES+=([$filename_]="${lines[*]}")
  57. lines_total=$(( ${lines_total} + ${#lines[@]} ))
  58. let files_total++
  59. fi
  60. done
  61. for filename in ${!FILEMATCHES[@]}; do
  62. echo "${esc}[35m${filename}${esc}[92m: ${esc}[31m${FILEMATCHES[$filename]}${esc}[0m"
  63. done
  64. printf "\nFound %d matching lines for pattern '%s' in %d files in package '%s'.\n\n" ${lines_total} "${2}" ${files_total} "${1}"