0

I would like to pick out lines from a file matching

# Mode: org

# can be any comment character for the programming language of the file.

Rather than #, in could also be any of ; or !, with any preceeding number of spaces using a bash script.

Examples

# Mode: org

   ;; Mode: org

!! Mode: org

One could figure out the language from the file extention

.el    emacs
.c     c
.cc    c++
.sh    bash
.rc    bash
.f     fortran
.F     fortran
.f90   fortran
.texi  texinfo
.tex   tex

This is the current bash function I am using

capture ()
{
 local efile="$1"

 local begorg endorg charcl

 impl="1"
 if [ "$impl" = "1" ]; then 
   charcl='^[[:space:]]*(#|;|!)+[[:space:]]*' 
 elif [ "$impl" = "2" ]; then
   charcl='^[[:space:]]*(//|@c)[[:space:]]*' 
 fi 
 
 begorg="${charcl}"'Mode: org$'
 endorg="${charcl}"'# End of org$'
 
 awk -v ccls="$charcl" -v bego="$begorg" -v endo="$endorg" \
   '$0 ~ bego { found=1; next } 
    $0 ~ endo { found=0; } 
    found { sub(/ccls/,""); print }' "$efile"
}

Would be better to this in awk, but I struggle on how to include the pattern.

/<pattern>/ { set variable to A }
/other pattern/ { set variable to B }
... use variable in sub
Vera
  • 1,173
  • 4
  • 17
  • If it depends on the programming language it would be better to perform the commands according to the language. What extension have those files, paired with the corresponding language comments format? [edit] the question adding this information, don't do it in the comments. Also, have you tried something so far? – schrodingerscatcuriosity Nov 03 '21 at 12:40
  • How can get first character from a match with pattern `^[[:space:]]*(#|;|!)+[[:space:]]*Mode: org$` – Vera Nov 03 '21 at 13:41
  • Does this answer your question? [Removing leading \`# \` with awk](https://unix.stackexchange.com/questions/675817/removing-leading-with-awk) – alecxs Nov 04 '21 at 18:07

0 Answers0