Capturing comment character from matching pattern
0
votes
0
answers
52
views
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.
// { set variable to A }
/other pattern/ { set variable to B }
... use variable in sub
Asked by Vera
(1363 rep)
Nov 3, 2021, 12:35 PM
Last activity: Nov 3, 2021, 01:20 PM
Last activity: Nov 3, 2021, 01:20 PM