0

I have the following bash function to print between two line numbers, for files watching file types .texi and .org recursively in a particular directory.

I would like to be able to supply the file extensions to search for using the -e option using , as delimiter. Example: -e el,texi,org

print-region ()
{

  # Process command line options
  shortopts="hvd:e:p:q:"
  longopts="help,version,directory:,extension:,startline:,stoplinene:,"

  opts=$(getopt -o "$shortopts" -l "$longopts" -n "$(basename $0)" -- "$@")
  if [ $? -eq 0 ]; then
    eval "set -- ${opts}"
    while [ $# -gt 0 ]; do
      case "$1" in
        --)
          shift;  break  ;;
        -h|-\?|--help)
          help=1
      local -r f=0
      break
      ;;
        -v|--version)
      version=1;  shift;  break  ;;
        -d|--directory)
      local -r dir=$2
      shift 2
      ;;
        -e|--extension)
      fltype=$2
      shift 2
      ;;
        -p|--startline)
      local -r na=$2;  shift 2  ;;
        -q|--stopline)
      local -r nb=$2;  shift 2  ;;
      esac
    done
  else
    shorthelp=1 # getopt returned (and reported) an error.
  fi

  if [[ $na =~ ^[[:digit:]]+$ ]] && [[ $nb =~ ^[[:digit:]]+$ ]]  \
  && [[ -d $dir ]]; then
    
    find "$dir" \( -name \*.org -o -name \*.texi \)  \
      -exec awk -v a="$na" -v b="$nb"                \
            'FNR == 1 {s="\n==> "FILENAME" <==\n"; f = 0}
             FNR == a {print s; f = 1}
             f {print; if (FNR == b) nextfile}' {} +
  fi
}
6
  • Which command do you want to use with the -e option? By the way, to print text between two line numbers, sed -n ${NUM1},${NUM2}p is a simpler solution than your awk program. Jul 7, 2021 at 5:59
  • Have thought using sed,but that would require calling sed for each file, whereas awk can take any number of files.
    – Pietru
    Jul 7, 2021 at 6:03
  • I have put -e|--extension in the getopt section. I would pass something like -e texi,org,el using comma as delimiter for different file types.
    – Pietru
    Jul 7, 2021 at 6:05
  • It's not quite clear what your question is. What problem do you have? You don't know how to parse org,texi,el? Jul 7, 2021 at 6:07
  • Am having difficulty passing the file types (extensions) to the find command. Would need to pass -name "*$ext" -o and collect the whole thing to an array.
    – Pietru
    Jul 7, 2021 at 6:10

1 Answer 1

0

Have had an initial go at a solution, and would mucd appreciate comments. Felicitations.

  str=$(echo $fltype | tr "," "\n")

  nmser=( '(' )
  for ext in $str; do
    nmser+=( -name "*.$ext" -o )
  done
  nmser[${#nmser[@]}-1]=')'
  
  printf "nmser:\n"
  for i in ${nmser[@]}; do echo $i; done

  if [[ $na =~ ^[[:digit:]]+$ ]] && [[ $nb =~ ^[[:digit:]]+$ ]]  \
  && [[ -d $dir ]]; then

    # `-exec` avoids problems with too many file matches 
    # `nextfile` stops processing the current input file.  
    #find "$dir" \( -name \*.org -o -name \*.texi \)  \
    
    find "$dir" "${nmser[@]}"  \

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .