I have some files that have the following construct in them. I want to print the whole sections present. The input would be other a title or keywords.
## DN [TITLE] KEYWORD,KEYWORD
## text line
## another text line
## DN [TITLE] ends here
Consider this file
some text
DN [Opcon] bash,recources
text line
another text line
DN [Opcon] ends here
more text
With the user specifying either Opcon, bash or recources, the section gets printed in the terminal.
To get the output
## DN [Opcon] bash,recources
## text line
## another text line
## DN [Opcon] ends here
For the search pattern I have constructed the following, ptn being the pattern matching
## DN [TITLE] KEYWORD,KEYWORD.
dpn='[[:space:]]*([#;!]+|@c|//)[[:space:]]DN[[:space:]]\[.*\]'
kpn='[[:space:]][^,]+(,[^,]+)*'
ptn="^($dpn)($kpn)?$"
Have done an initial try with awk
dn_ere='^[[:space:]]*([#;!]+|@c|//)[[:space:]]DN[[:space:]]\[.*\]'
beg_ere="${dn_ere} ${keyword}$"
end_ere="${dn_ere} ends here$"
awk -v begpn="$beg_ere" -v endpn="$end_ere"
'$0 ~ begpn { insc=1; next }
$0 ~ endpn { insc=0; print "" }
insc { print }' "$efile"
The difficulty is to match the keyword in beg_ere because I would not know whether the match is in the first keyword, or the second, etc.
awkorsedto match the sections defined by theDNstructure, but only when there is atitleorkeywordmatch. For instance, I would ask to print allDNsections containing keywordresourceor titleOpcon. – Vera Jan 31 '23 at 00:13