script pentru cautat text

4 posts / 0 new
Last post
mandi
mandi's picture
script pentru cautat text

N-am reusit sa gasesc un search bun, asa ca am facut un script: m-grep.sh (adica mime-grep, nu mandi-grep 8) )
Orice sugestii si mai ales reclamatii binevenite. Poate e util cuiva, eu l-am pus in /usr/local/bin.

Sarbatori Fericite tuturor!

OT. http://forum.mandriva.com/viewtopic.php?t=126785
Trebuie sa-mi eliberez o partitie, a aparut beta

LE Trebuie eliminate si :( Atasat .txt
Edit 2: Am adaugat si *.bz2, doar pentru text.

Update: adaugat un counter si simplificat

[code]#!/bin/bash
## m-grep.sh ##-v.094-69 MRB##
# Finds at least one match of a given string in files or filenames (from a specified folder)
# asks for 'pdftotext' from poppler: http://poppler.freedesktop.org
# asks for 'mimetype' from perl-file-mimeinfo: http://search.cpan.org/dist/File-MimeInfo/
# asks for 'html2text' from http://www.mbayer.de/html2text
##############################
START=$(date +%s)
BADARGS=65
# Check search string&path
if [ $# -le 0 -o $# -ge 3 ] ; then
echo "Usage: `basename $0` search_string [path]"
echo "* if search_string is a phrase, use \"quotes\" * also avoid special chars '&,$,!,',\",\\....' inside strings *"
exit $BADARGS
elif [ $# = 2 ] ; then
directory="$2"
else directory=${PWD} ; fi
if [ -x "$directory" ] ; then
echo 'Searching in:' $directory
else
echo \"$directory\" "is not a valid search path, Zarro files searched"
exit $BADARGS ; fi
OLDIFS=$IFS; # Backup IFS if any
IFS=:
# define some functions
fstring="$1"
count_f=0
count_ff=0
count_f() { let count_f++ ; }
count_ff() { let count_ff++ ; }
searching() {
for file in $( find $directory -mount -type f -name '*' -printf "%p$IFS" 2>/dev/null | sort -bif ) ; do
if [[ ! -z $(zgrep -ialsE --devices=skip -e "$fstring" $file 2>/dev/null) ]] ; then
echo $file ; count_ff ; fi
count_f
continue # next one
done ;
}
# do some search
if ! which pdftotext &> /dev/null ; then
echo "*!* poppler is not installed * Can't use mimetypes... :( "
searching
elif ! which mimetype &> /dev/null ; then
echo "*!* mimetype not found * Can't use mimetypes... :( "
searching
elif ! which html2text &> /dev/null ; then
echo "*!* html2text not installed * Can't use mimetypes... :( "
searching
else
for file in $( find $directory -mount -type f -name '*' -printf "%p$IFS" 2>/dev/null | sort -bif ) ; do
count_f
case `mimetype -iM $file | awk -F": " '{ print $2 }'` in
application/x-executable | application/x-sharedlib | application/java-archive | inode/symlink) ;;
application/pdf)
if [[ ! -z $( pdftotext -q -nopgbrk $file - | egrep -ials --devices=skip -e "$fstring" 2>/dev/null ) ]] ; then
echo $file ; count_ff ; fi ; ;;
application/vnd.oasis.opendocument.*)
if [[ ! -z $(unzip -pqq $file content.xml | html2text -nobs -ascii | egrep -ials --devices=skip -e "$fstring" 2>/dev/null ) ]] ; then
echo $file ; count_ff ; fi ; ;;
application/x-bzip)
if [[ ! -z $(bunzip2 -cq $file | zgrep -ialsE --devices=skip -e "$fstring" 2>/dev/null ) ]] ; then
echo $file ; count_ff ; fi ; ;;
*)
if [[ ! -z $(zgrep -ialsE --devices=skip -e "$fstring" $file 2>/dev/null) ]] ; then
echo $file ; count_ff ; fi ; ;;
esac
continue # next one
done ;
fi
if [ $count_f -ne "0" ] ; then
let "value = $(date +%s) - $START"
echo "[$fstring] found $count_ff time(s) while reading $count_f file(s) in $value seconds on $(date +%a,%x-%T)"
else
echo "Zarro files searched" ; fi
IFS=${OLDIFS} # Restore IFS if any
exit $?
[/code]

symbianflo
symbianflo's picture
Fa-i si o interfata in ce

Fa-i si o interfata in ce vrei tu si il impachetez imediat pe mrb. :p
PS: as aduga :

echo Greetings from all the MRB Team
echo bye, bye

suna misto la sfirsit ;)


http://www.mandrivausers.ro/ MRB aint no shit , :p Linux user number 507107 Nu dați banii pe prostii, puneți Linux pe PISI ![ARLUG]

Unix is user-friendly. It’s just very selective about who its friends are

mandi
mandi's picture
Merci pt. feedback. Ce sa-i

Merci pt. feedback. Ce sa-i fac? interfata? eu abia aflu cum merge 'ls -al', lol
Daca-i bun de ceva, modificati, adaugati, impachetati etc Multumiri.

mandi
mandi's picture
Tot eu - oricum ultima

Tot eu - oricum ultima modificare pe'aici daca nu mai e nimeni interesat.
A mai fost ceva asemanator - rexgrep - e in repo, ultima varianta din '99, scris in C, nu pricep nimic :(
mimetype e optional acum ; adaugat si *.zip (.skz); prin ce nu stie sa caute: *.ppt (si probabil multe altele..)
Acum si un preview cu GUI la cerere :P (- parerea mea ca-i cam pacat de el :))
(trebuie testat si mesajele se pot modifica)