Advertisement
Kevin_Zhang

Untitled

May 29th, 2022
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 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.  
  11. ER () {
  12.     echo $1
  13.     exit 1
  14. }
  15.  
  16. while [[ $# > 0 ]]; do
  17.     arg=$1
  18.     shift
  19.     case $arg in
  20.         -name)
  21.             NAME_REGEX=$1
  22.             shift
  23.         ;;
  24.         -l)
  25.             SEG=$1
  26. ## TODO parse the info
  27.             if [[ $SEG =~ .*:.* ]]; then
  28.                 ## two parameters
  29.                 LINE_L=${SEG%:*}
  30.                 LINE_R=${SEG##*:}
  31.             else
  32.                 LINE_L=$SEG
  33.                 LINE_R=$SEG
  34.             fi
  35.             if (($LINE_L == 0)); then
  36.                 ER "LINE cannot be 0 sorry QQ"
  37.             fi
  38.             shift
  39.         ;;
  40.         -sep)
  41.             DLIM=$1
  42.             shift
  43.         ;;
  44.         -c)
  45.             WORDLIST=$1
  46.             HAS_WORD="true"
  47.             shift
  48.         ;;
  49.         *)
  50.             DIR=$arg
  51.         ;;
  52.     esac
  53. done
  54.  
  55. WORDLIST=$(tr $DLIM " " <<< $WORDLIST)
  56.  
  57. for i in $(find $DIR -type f); do
  58.     if ! [[ $(basename $i) =~ $NAME_REGEX ]]; then
  59.         continue
  60.     fi
  61.     echo "Processing file $(basename $i):"
  62.  
  63.     ALL=$(wc -l < $i)
  64.     L=$LINE_L
  65.     R=$LINE_R
  66.  
  67.     if (( $L < 0 )); then
  68.         L=$((ALL+L+1))
  69.         if (( $L < 0 )); then
  70.             L=1
  71.         fi
  72.     fi
  73.  
  74.     if (( $R < 0 )); then
  75.         R=$((ALL+R+1))
  76.     fi
  77.  
  78.     if (( $L > $R )); then
  79.         continue
  80.     fi
  81.  
  82.     if [[ $HAS_WORD == "true" ]]; then
  83.         CONT=$(sed -n "${L},${R}p" < $i)
  84.         for w in $WORDLIST; do
  85.             echo " Word $w occurs: $(grep -o $w <<< $CONT | wc -l) times"
  86.         done
  87.     else
  88.         sed -n "${L},${R}p" < $i
  89.     fi
  90. done
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement