Using comment symbol for matching comment sections
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to use a variable for character class. And also take care of C-Language comments //
Capital `C` is for Fortran Fixed Form files.
For single comment characters `[#;!C]` I would like the possibility any number of comment characters, hence `([#;!C])+`. But for other comment types (e.g. `//` for C-Language, and `@c` for Texinfo-Language) there will be no repeats, thus just
`[//|@c]`.
Code:
capture ()
{
local efile="$1"
local begorg endorg charcl
i="1"
if [ "$i" = "1" ]; then
charcl='^[[:space:]]*(#|;|!)+[[:space:]]*'
elif [ "$i" = "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"
}
I agree. Have updated the function but have to recognise the language (whether `#`, `;`, `!`).
To avoid too complicated patterns, the if statement can be used to distinguish between single character comments (#;!) and double character comments (//|@c).
Have modified to check for any of the comment cases in a single regex.
Code:
capture ()
{
local efile="$1"
local charcl begorg endorg
charcl='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*'
begrec="${charcl}"'Mode: org$'
endrec="${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"
}
But when I tried it, the comment characters are still displayed, rather than deleted.
Code:
awk: warning: escape sequence `\/' treated as plain `/'
# Assigns shell positional parameters or changes the values of shell
# options. The -- option assigns the positional parameters to the
# arguments of {set}, even when some of them start with an option
# prefix `-'.
; Assigns shell positional parameters or changes the values of shell
; options. The -- option assigns the positional parameters to the
; arguments of {set}, even when some of them start with an option
; prefix `-'.
@c Assigns shell positional parameters or changes the values of shell
@c options. The -- option assigns the positional parameters to the
@c arguments of {set}, even when some of them start with an option
@c prefix `-'.
This was the input
Code:
## Mode: org
# Assigns shell positional parameters or changes the values of shell
# options. The -- option assigns the positional parameters to the
# arguments of {set}, even when some of them start with an option
# prefix `-'.
## # End of org
;; Mode: org
; Assigns shell positional parameters or changes the values of shell
; options. The -- option assigns the positional parameters to the
; arguments of {set}, even when some of them start with an option
; prefix `-'.
;; # End of org
@c Mode: org
@c Assigns shell positional parameters or changes the values of shell
@c options. The -- option assigns the positional parameters to the
@c arguments of {set}, even when some of them start with an option
@c prefix `-'.
@c # End of org
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.