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.

66 lines
1.7 KiB

  1. #!/bin/bash
  2. ##################################################
  3. # Find line patterns in package files.
  4. # Print matching file path and matching lines.
  5. function help() {
  6. echo -e "Usage: findinpkg <pkgname> <pattern-to-search>"
  7. exit
  8. }
  9. if [[ ${#@} -ne 2 ]]; then
  10. help
  11. fi
  12. if [[ ! $(pacman -Qq "${1}" 2> /dev/null) ]]; then
  13. echo "Can't find package ${1}"
  14. help
  15. fi
  16. filematches=()
  17. lines_total=0
  18. files_total=0
  19. typeset -A FILEMATCHES
  20. esc=$(printf '\033')
  21. for file in $(pacman -Qlq "${1}"); do
  22. if [[ -f "${file}" ]]; then
  23. filestr=$(grep --line-number --with-filename --binary-files=without-match -i "${2}" "${file}" | sed -r "s/^(.*?:)([0-9]+):.*/\1\2/g")
  24. # In some rare cases grepped files are presented on the same line
  25. for filestr in ${filestr[@]}; do
  26. filematches+=(${filestr})
  27. done
  28. fi
  29. done
  30. for filematch in ${filematches[@]}; do
  31. filename_=$(printf '%s' "${filematch}" | sed -r 's/^(.*?):[0-9]+$/\1/')
  32. if [[ ${FILEMATCHES[$filename_]} == "" ]]; then
  33. lines=()
  34. echo "Processing file: ${filename_}"
  35. # Get all occurences
  36. singlefile=$(echo ${filematches[@]} | grep -oE "(${filename_}:[0-9]+)" )
  37. for occurence in ${singlefile[@]}; do
  38. linenum=$(printf '%s' "${occurence}" | sed -r 's/^.*?:([0-9]+$)/\1/')
  39. lines+=(${linenum})
  40. done
  41. FILEMATCHES+=([$filename_]="${lines[*]}")
  42. lines_total=$(( ${lines_total} + ${#lines[@]} ))
  43. let files_total++
  44. fi
  45. done
  46. for filename in ${!FILEMATCHES[@]}; do
  47. echo "${esc}[35m${filename}${esc}[92m: ${esc}[31m${FILEMATCHES[$filename]}${esc}[0m"
  48. done
  49. printf "\nFound %d matching lines for pattern '%s' in %d files in package '%s'.\n\n" ${lines_total} "${2}" ${files_total} "${1}"