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.
But I am getting more lines being printed when I pass the same file where I have the code and thus the function `capture-org`. This is because the script gets a match with `## mode` in the code part (i.e. the matches are matching occurrence anywhere on the line).
I need to match exactly as with the patterns ^[[:space:]]*## mode: org$ and ^[[:space:]]*## # End of org$
awk '/^[[:space:]]*## mode: rec$/ { flag=1; next }
/^[[:space:]]*## # End of rec$/ { flag=0; }
flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
But I am getting the following but need the starting "# " removed.
Code:
# HDG: Handling function argument parsing with getopts
# Rmk: No call to {shift} is required with getopts
# Rmk: No hyphen {-} required when searching option names
# + Case patterns do not start with option hyphen {-} because
# + getopts strips off the hyphen and makes the value of {arg}
# + to be just the option letter.
# Rmk: Separating options from non-options with --
# + {getopts} stops processing options when the argument is not
# + defined as an option in {shortopts}; or if the argument is
# + "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
# + An option value can be -- without it being considered a
# + separator between options and non-options.
# + Example { edvart-getopts -g "--" }.
# Rmk: Explicitly testing for {--}
# + There is no need to test for {--} when using {getopts}.
# HDG: Silent Error Reporting Mode (SERM) in getopts
# Rmk: Detects warnings without printing built-in messages.
# Rmk: Enabled by colon {:} as first character in shortopts.
You're close. The method for that is a little non-obvious:
Code:
begrec='^[[:space:]]*## mode: rec$'
endrec='^[[:space:]]*## # End of rec$'
awk -v begr="$begrec" -v endr="$endrec" \
'$0~begr { flag=1; next }
$0~endr { flag=0; }
flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
That comes down to the shortcuts and abbreviations that AWK allows. The short annotation for $0~/foo/ is simply /foo/. Be sure to check the reference manual for the AWK scripting language periodically during this task: "man awk"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.