[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

chercher à l'intérieur des fichiers pdf/doc/xls/odf/sh/txt/... en mode 'graphique'



'lut,

Pour re-rebondir à propos de la demande de Steve. J'ai concocté il y a un petit bout de temps, un utilitaire en mode graphique (search_pattern), avec yad, permettant de rechercher un motif dans tout type de fichier.

search_pattern.sh se base sur /usr/bin/uvgrep, que j'ai un peu modifié, qui lui même nécessite grep / pdfgrep / unzip / libxml2-utils

bon we ;)

f.

Attachment: search_pattern.sh
Description: application/shellscript

#!/bin/bash

# uvgrep (UniVersal Grep), version 1.0.1 (2016/07/08)
# (c) Hans-Georg Esser, EasyLinux, hgesser@easylinux.de
# uvgrep: grep txt, PDF and LibreOffice files
# Licensed under the GPL, version 3 (see LICENSE)

#nécessite grep / pdfgrep / unzip / libxml2-utils (pour xmllint)

MSG_NOTFOUND="$0: %s: Donnée non trouvée\n"
MSG_BADFILETYPE="$0: %s: Type de fichier non supporté\n"
MSG_BADOPTION="$0: Mauvaise Option '%s'\n"

unset OPT_IGNORE
unset OPT_NUMBER
OPTERR=0
IFS=""
OPTIONS=""
ODTOPTIONS=""
XMLLINT_INDENT=""
while getopts ":in" option; do
  case $option in
    "?")
      printf "$MSG_BADOPTION" "-$OPTARG"
      exit 1;;
    "i")
      OPTIONS+=i
      ODTOPTIONS+=i
      OPT_IGNORE=1;;
    "n")
      OPTIONS+=n
      OPT_NUMBER=1;;
  esac
done
if [ "$OPTIONS" != "" ]; then OPTIONS="-$OPTIONS"; fi
if [ "$ODTOPTIONS" != "" ]; then ODTOPTIONS="-$ODTOPTIONS"; fi
shift $((OPTIND-1))

PATTERN=$1
shift

for file in $@; do
  if [ ! -f "$file" ]; then
    #printf "$MSG_NOTFOUND" "$file" >&2
    continue
  fi
  
  ext="${file##*.}"
  filetype=$(echo $ext | tr 'A-Z' 'a-z')
  case $filetype in
    txt | sh | conf | pl | py | php | css | js | csv )
      #grep -H --color $OPTIONS $PATTERN "$file" ;;
      grep -l $OPTIONS $PATTERN "$file" ;;
      
    pdf)
      #pdfgrep -H $OPTIONS $PATTERN "$file" ;;
      pdfgrep -H $OPTIONS $PATTERN "$PWD/$file" | cut -d":" -f1 | uniq ;;
			
    odt | ods | odp | odg)
#        unzip -caq "$file" content.xml | xmllint --format - | sed -e 's/ *//' \
#          | grep $ODTOPTIONS -H --color=always $PATTERN | sed -e "s|(entrée standard)|$file|" ;;

        unzip -caq "$file" content.xml | xmllint --format - | sed -e 's/ *//' \
          | grep $ODTOPTIONS -H $PATTERN | sed -e "s|(entrée standard)|$PWD/$file|" | cut -d":" -f1 | uniq ;;

    docx)
      unzip -caq "$file" word/document.xml | xmllint --format - | sed -e 's/ *//' \
        | grep $ODTOPTIONS -H --color=always $PATTERN | sed -e "s|^(standard input)|$PWD/$file|" ;;

    xlsx)
      dir=$(tempfile -p uvgrep-); rm $dir; mkdir $dir
      unzip -ajq -d $dir "$file" xl/worksheets/sheet*.xml xl/sharedStrings.xml
      pushd $dir > /dev/null
      for subfile in *.xml; do
        xmllint --recover --format "$subfile" | sed -e 's/ *//' \
        | grep $ODTOPTIONS -H --color=always $PATTERN | sed -e "s|^(standard input)|$PWD/$file[/$subfile]|"
      done
      popd > /dev/null
      rm -rf $dir ;;

    pptx)
      dir=$(tempfile -p uvgrep-); rm $dir; mkdir $dir
      unzip -ajq -d $dir "$file" ppt/slides/slide*.xml
      pushd $dir > /dev/null
      for subfile in *.xml; do
        xmllint --recover --format "$subfile" | sed -e 's/ *//' \
        | grep $ODTOPTIONS -H --color=always $PATTERN | sed -e "s|^(standard input)|$PWD/$file[/$subfile]|"
      done
      popd > /dev/null
      rm -rf $dir ;;

    *)
      printf "$MSG_BADFILETYPE" "$file" "$filetype" >&2 ;;
  esac
done

echo "(fin de la recherche)"

Reply to: