Advertisement
Guest User

fuzzyfind

a guest
Jul 21st, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. perm() {
  4.     # frei nach http://stackoverflow.com/questions/3846123/generating-permutations-using-bash
  5.     local items="$1"
  6.     local out="$2"
  7.     local i
  8.     [[ "$items" == "" ]] && permutation[j++]="$out*" && return
  9.     for (( i=0; i<${#items}; i++ )) ; do
  10.         perm "${items:i+1}" "$out*${items:0:i+1}"
  11.     done
  12. }
  13.  
  14. add_find(){
  15.     while read -rd ''
  16.     do
  17.         erg[k++]="$REPLY"
  18.     done < <(find . -type f -iwholename "$1" -print0 2>/dev/null)
  19. }
  20.  
  21. sort_depth(){
  22.     arr=( "${erg[@]}" )
  23.     l=0
  24.     for ((i=1; i<=$maxdepth; i++))
  25.     do
  26.         for ((j=0; j<$k; j++))
  27.         do
  28.             count="${arr[j]//[^\/]/}"
  29.             [[ "${#count}" == "$i" ]] && erg[l++]="${arr[j]}"
  30.         done
  31.     done
  32. }
  33.  
  34. t1="$(date +%s)"
  35. string="$1"
  36. length="$(echo $string | wc -m)"
  37. k=0
  38. maxdepth=15
  39. perm "$string"
  40. starcount=1
  41. while [ $((starcount-1)) -lt "$length" ] && [ $k -lt 1 ]
  42. do
  43.     for i in "${permutation[@]}"
  44.     do
  45.         count="${i//[^\*]/}"
  46.         [[ "${#count}" == "$starcount" ]] && add_find "$i" && printf "%b" "$i\r"
  47.     done
  48.     t2="$(date +%s)"
  49.     [[ $((t2-t1)) -gt 30 ]] && break
  50.     ((starcount++))
  51. done
  52.  
  53. sort_depth
  54. t2="$(date +%s)"
  55. printf "%${length}s\r"
  56. echo "Zeit: $((t2-t1)) s"
  57.  
  58. for i in "${erg[@]}"
  59. do
  60.     echo "$i"
  61. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement