Advertisement
Kevin_Zhang

Untitled

May 29th, 2022
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. NAME_REGEX=".*"
  4. LINE_L=1
  5. LINE_R=-1
  6. DIR="."
  7. DLIM=""
  8. HAS_WORD="false"
  9. WORDLIST=""
  10. COND="false"
  11. COMP=">= <= > < ="
  12.  
  13. ER () {
  14.     echo $1
  15.     exit 1
  16. }
  17.  
  18. while [[ $# > 0 ]]; do
  19.     arg=$1
  20.     shift
  21.     case $arg in
  22.         -name)
  23.             NAME_REGEX=$1
  24.             shift
  25.         ;;
  26.         -l)
  27.             SEG=$1
  28.             if [[ $SEG =~ .*:.* ]]; then
  29.                 ## two parameters
  30.                 LINE_L=${SEG%:*}
  31.                 LINE_R=${SEG##*:}
  32.             else
  33.                 LINE_L=$SEG
  34.                 LINE_R=$SEG
  35.             fi
  36.             if (($LINE_L == 0)); then
  37.                 ER "LINE cannot be 0 sorry QQ"
  38.             fi
  39.             shift
  40.         ;;
  41.         -sep)
  42.             DLIM=$1
  43.             shift
  44.         ;;
  45.         -s)
  46.             COND="true"
  47.         ;;
  48.         -c)
  49.             WORDLIST=$1
  50.             HAS_WORD="true"
  51.             shift
  52.         ;;
  53.         *)
  54.             DIR=$arg
  55.         ;;
  56.     esac
  57. done
  58.  
  59. WORDLIST=$(tr "," " " <<< $WORDLIST)
  60.  
  61. ALLG=0
  62. ALLB=0
  63. for i in $(find $DIR -type f); do
  64.     if ! [[ $(basename $i) =~ $NAME_REGEX ]]; then
  65.         continue
  66.     fi
  67.     echo "Processing file $(basename $i):"
  68.  
  69.     ALL=$(wc -l < $i)
  70.     L=$LINE_L
  71.     R=$LINE_R
  72.  
  73.     if (( $L < 0 )); then
  74.         L=$((ALL+L+1))
  75.         if (( $L < 0 )); then
  76.             L=1
  77.         fi
  78.     fi
  79.  
  80.     if (( $R < 0 )); then
  81.         R=$((ALL+R+1))
  82.     fi
  83.  
  84.     if (( $L > $R )); then
  85.         continue
  86.     fi
  87.  
  88.     CONT=$(sed -n "${L},${R}p" < $i)
  89.     if [[ $HAS_WORD == "true" ]]; then
  90.  
  91.         cok="true"
  92.  
  93.         for word_block in $WORDLIST; do
  94.             w=$word_block
  95.             CUR_CMP=">"
  96.             CMP_T=0
  97.             if [[ $COND == "true" ]]; then
  98.                 for cmp_w in ${COMP}; do
  99.                     NC=$(sed "s/$cmp_w/ /" <<< "$word_block")
  100.                     if [[ $NC != $word_block ]]; then
  101.                         w=${word_block%${cmp_w}*}
  102.                         CMP_T=${word_block##*${cmp_w}}
  103.                         CUR_CMP=$cmp_w
  104.                         break
  105.                     fi
  106.                 done
  107.             fi
  108.  
  109.             T=0
  110.             if [[ $DLIM == "" ]]; then
  111.                 T=$(grep -o "$w" <<< "$CONT" | wc -l)
  112.             else
  113.                 while IFS="$DLIM" read -ra ADDR; do
  114.                     for b in "${ADDR[@]}"; do
  115.                         if [[ $b == $w ]]; then
  116.                             ((++T))
  117.                         fi
  118.                     done
  119.                 done <<< "$CONT"
  120.             fi
  121.  
  122.             if [[ $COND == "true" ]]; then
  123.                 res="false"
  124.                 case $CUR_CMP in
  125.                     "=")
  126.                         if (($T == $CMP_T)); then
  127.                             res="true"
  128.                         fi
  129.                     ;;
  130.                     ">=")
  131.                         if (($T >= $CMP_T)); then
  132.                             res="true"
  133.                         fi
  134.                     ;;
  135.                     ">")
  136.                         if (($T > $CMP_T)); then
  137.                             res="true"
  138.                         fi
  139.                     ;;
  140.                     "<=")
  141.                         if (($T <= $CMP_T)); then
  142.                             res="true"
  143.                         fi
  144.                     ;;
  145.                     "<")
  146.                         if (($T < $CMP_T)); then
  147.                             res="true"
  148.                         fi
  149.                     ;;
  150.                 esac
  151.                 if [[ $res != "true" ]]; then
  152.                     cok="false"
  153.                 fi
  154.             else
  155.                 echo " Word $w occurs: $T times"
  156.             fi
  157.         done
  158.  
  159.         if [[ $cok == "true" ]]; then
  160.             ((++ALLG));
  161.         else
  162.             ((++ALLB));
  163.         fi
  164.     else
  165.         sed -n "${L},${R}p" < $i
  166.     fi
  167. done
  168.  
  169. if [[ $COND == "true" ]] && [[ $HAS_WORD == "true" ]] ; then
  170.     echo -e "Match all conditions\t: $ALLG files"
  171.     echo -e "doesn't Match conditions\t: $ALLB files"
  172. fi
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement