Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- NAME_REGEX=".*"
- LINE_L=1
- LINE_R=-1
- DIR="."
- DLIM=""
- HAS_WORD="false"
- WORDLIST=""
- COND="false"
- COMP=">= <= > < ="
- ER () {
- echo $1
- exit 1
- }
- while [[ $# > 0 ]]; do
- arg=$1
- shift
- case $arg in
- -name)
- NAME_REGEX=$1
- shift
- ;;
- -l)
- SEG=$1
- if [[ $SEG =~ .*:.* ]]; then
- ## two parameters
- LINE_L=${SEG%:*}
- LINE_R=${SEG##*:}
- else
- LINE_L=$SEG
- LINE_R=$SEG
- fi
- if (($LINE_L == 0)); then
- ER "LINE cannot be 0 sorry QQ"
- fi
- shift
- ;;
- -sep)
- DLIM=$1
- shift
- ;;
- -s)
- COND="true"
- ;;
- -c)
- WORDLIST=$1
- HAS_WORD="true"
- shift
- ;;
- *)
- DIR=$arg
- ;;
- esac
- done
- WORDLIST=$(tr "," " " <<< $WORDLIST)
- ALLG=0
- ALLB=0
- for i in $(find $DIR -type f); do
- if ! [[ $(basename $i) =~ $NAME_REGEX ]]; then
- continue
- fi
- echo "Processing file $(basename $i):"
- ALL=$(wc -l < $i)
- L=$LINE_L
- R=$LINE_R
- if (( $L < 0 )); then
- L=$((ALL+L+1))
- if (( $L < 0 )); then
- L=1
- fi
- fi
- if (( $R < 0 )); then
- R=$((ALL+R+1))
- fi
- if (( $L > $R )); then
- continue
- fi
- CONT=$(sed -n "${L},${R}p" < $i)
- if [[ $HAS_WORD == "true" ]]; then
- cok="true"
- for word_block in $WORDLIST; do
- w=$word_block
- CUR_CMP=">"
- CMP_T=0
- if [[ $COND == "true" ]]; then
- for cmp_w in ${COMP}; do
- NC=$(sed "s/$cmp_w/ /" <<< "$word_block")
- if [[ $NC != $word_block ]]; then
- w=${word_block%${cmp_w}*}
- CMP_T=${word_block##*${cmp_w}}
- CUR_CMP=$cmp_w
- break
- fi
- done
- fi
- T=0
- if [[ $DLIM == "" ]]; then
- T=$(grep -o "$w" <<< "$CONT" | wc -l)
- else
- while IFS="$DLIM" read -ra ADDR; do
- for b in "${ADDR[@]}"; do
- if [[ $b == $w ]]; then
- ((++T))
- fi
- done
- done <<< "$CONT"
- fi
- if [[ $COND == "true" ]]; then
- res="false"
- case $CUR_CMP in
- "=")
- if (($T == $CMP_T)); then
- res="true"
- fi
- ;;
- ">=")
- if (($T >= $CMP_T)); then
- res="true"
- fi
- ;;
- ">")
- if (($T > $CMP_T)); then
- res="true"
- fi
- ;;
- "<=")
- if (($T <= $CMP_T)); then
- res="true"
- fi
- ;;
- "<")
- if (($T < $CMP_T)); then
- res="true"
- fi
- ;;
- esac
- if [[ $res != "true" ]]; then
- cok="false"
- fi
- else
- echo " Word $w occurs: $T times"
- fi
- done
- if [[ $cok == "true" ]]; then
- ((++ALLG));
- else
- ((++ALLB));
- fi
- else
- sed -n "${L},${R}p" < $i
- fi
- done
- if [[ $COND == "true" ]] && [[ $HAS_WORD == "true" ]] ; then
- echo -e "Match all conditions\t: $ALLG files"
- echo -e "doesn't Match conditions\t: $ALLB files"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement