0

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.

Vera
  • 1,223
  • 2
    Veak, you're generating so many questions people don't have time to answer one before you ask the next. Please consider using a shell scripting tutorial and learn something about writing code – Chris Davies Jan 31 '23 at 00:02
  • It is this question that remains. Usually people suggest to split a question into many small questions, so I did that. – Vera Jan 31 '23 at 00:03
  • 2
    @Veak people suggest that indeed, when it's the alternative to a tremendously complex single post. Your posts can all be answered with: please post the first of your small questions, and try to learn from the answers (that should solve the others, at least partially) – or as roaima suggests: you seem to be very handy with regular expressions, which in all honesty is the harder thing about the scripts you need to write. Maybe be more specific about the code you're missing – right now it looks like you need to get a shell scripting tutorial as answer to multiple of your questions :) – Marcus Müller Jan 31 '23 at 00:07
  • Please try to understand that people are definitely not here to solve your finely packaged sequence of tasks. We're all here to help you understand! And I don't know whether "learning" goes well with asking 13 questions in but two days, a lot of which were already duplicates or underresearched. – Marcus Müller Jan 31 '23 at 00:09
  • My actual problem in on how to use awk or sed to match the sections defined by the DN structure, but only when there is a title or keyword match. For instance, I would ask to print all DN sections containing keyword resource or title Opcon. – Vera Jan 31 '23 at 00:13

0 Answers0