Advertisement
Kevin_Zhang

Untitled

May 29th, 2022
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.90 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. NAME_REGEX=".*"
  4. LINE_L=1
  5. LINE_R=-1
  6. DIR="."
  7.  
  8. ER () {
  9.     echo $1
  10.     exit 1
  11. }
  12.  
  13. while [[ $# > 0 ]]; do
  14.     arg=$1
  15.     shift
  16.     case $arg in
  17.         -name)
  18.             NAME_REGEX=$1
  19.             shift
  20.         ;;
  21.         -l)
  22.             SEG=$1
  23. ## TODO parse the info
  24.             if [[ $SEG =~ .*:.* ]]; then
  25.                 ## two parameters
  26.                 LINE_L=${SEG%:*}
  27.                 LINE_R=${SEG##*:}
  28.             else
  29.                 LINE_L=$SEG
  30.                 LINE_R=$SEG
  31.             fi
  32.             if (($LINE_L == 0)); then
  33.                 ER "LINE cannot be 0 sorry QQ"
  34.             fi
  35.             shift
  36.         ;;
  37.         *)
  38.             DIR=$arg
  39.         ;;
  40.     esac
  41. done
  42.  
  43. for i in $(find $DIR -type f); do
  44.     if ! [[ $(basename $i) =~ $NAME_REGEX ]]; then
  45.         continue
  46.     fi
  47.     echo "Processing file $(basename $i):"
  48.  
  49.     ALL=$(wc -l < $i)
  50.     L=$LINE_L
  51.     R=$LINE_R
  52.  
  53.     if (( $L < 0 )); then
  54.         L=$((ALL+L+1))
  55.         if (( $L < 0 )); then
  56.             L=1
  57.         fi
  58.     fi
  59.  
  60.     if (( $R < 0 )); then
  61.         R=$((ALL+R+1))
  62.     fi
  63.  
  64.     if (( $L > $R )); then
  65.         continue
  66.     fi
  67.  
  68.     sed -n "${L},${R}p" < $i
  69.  
  70. done
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement