Have written some code in an awk file. Basically the code captures lines starting with ### DK
and returns the block between two blank lines. Would like the capability of either using a function or
directly via pattern-action.
But I get syntax error inside the conditionals.
awk: /home/mardonius/dk-search.awk:111: /^### DK/ {
awk: /home/mardonius/dk-search.awk:111: ^ syntax error
awk: /home/mardonius/dk-search.awk:117: printing {
awk: /home/mardonius/dk-search.awk:117: ^ syntax error
Here is the file's code
## dk-search.awk
function print_dk_sections(line)
{
## print_dk_sections
## Print all lines starting after a line beginning with
## "### DK" up to (but not including) the second empty line.
## Called for each line of input to control which lines are printed.
if (line ~ /^### DK/) {
printing = 1
empty_count = 0
return 0
}
if (printing) {
print line
if (line ~ /^$/) {
empty_count++
if (empty_count == 2) { printing = 0 }
}
}
return 1
}
{
## Preferred default mode due to better modularity and
## maintainability using a dedicated function to process lines.
if (mode == "fn") {
print_dk_sections($0)
}
## ------------------------------------------------------------
## Idiomatic and efficient but less modular than using a function.
## Uses pattern-action blocks directly inside the condition.
else if (mode == "direct-pa") {
/^### DK/ {
printing = 1
empty_count = 0
next # Skip printing this line
}
printing {
print
if ($0 ~ /^$/) {
empty_count++
if (empty_count == 2) { printing = 0 }
}
}
}
}
The input file would look like this
## ###############################################################
##
### DK2 ⛯ opcon-search ()
opcon-search ()
{
## opcon-search DIR STRING
## Recursively search for STRING in all files under a directory
## DIR. Prints the name of each file containing matches, followed
## by matching lines with line numbers.
local search_dir=$1
local search_str=$2
}
## ###############################################################
##
### DK2 ⛯ opcon-trisquel-install ()
opcon-trisquel-install ()
{
## opcon-trisquel-install
## Textual installer guide for Trisquel GNU with LVM. Provides
## practical advice and commands to help users perform a secure
## and flexible installation. References another command
## opcon-trisquel-root for advanced partition resizing
## instructions.
lexicon="
1. Start the Trisquel Installer and Select LVM
- Boot from the Trisquel Installation Media.
- Begin the Installation Process."
}
## ###############################################################
##
### DK3 ⛯ opcon-trisquel-root
opcon-trisquel-root ()
{
## opcon-trisquel-root
## Simply prints some text.
lexicon="
1. TODO"
printf '%s\n' "$lexicon"
}
/^### DK/ {...
, useif( /^### DK/ ) {...
if ($0 ~ /^### DK/) {
but perhaps the$0 ~
would net be required,$0 ~
is not required.if ($0 ~ /^$/) {
?