0

I have files with sections defined by the starting section composed oy TITLE, SUBTITLE, followed by comma separated KEYWORD.

## TITLE [SUBTITLE] KEYWORD,KEYWORD  

The ending is done using

## END OF TITLE [SUBTITLE]

I want to ensure that the file contains the corresponding closing part to the definition.

How can I make a test that check the file has things as should be. I need the test in bash.

## FAML [ASMB] keyword,keyword  

## Some text
## Description
## END OF FAML [ASMB]

Some Code

## More text

## FALUN [GONG] keyword,keyword  

## Some text
## Description
## END OF FALUN [GONG]

More Text

Have started with following to capture the actual strings for the corresponding section.

while read line; do
  if [[ $line =~ ^##\ ([A-Z]+)\ \[([A-Z]+)\]\ (.*),(.*)$ ]]; then
    title=${BASH_REMATCH[1]}
    subtitle=${BASH_REMATCH[2]}
    keywords=${BASH_REMATCH[3]}
    keywords2=${BASH_REMATCH[4]}
    echo "Title: $title"
    echo "Subtitle: $subtitle"
    echo "Keywords: $keywords, $keywords2"
  fi
done < input.txt

Attempted to run the code on the following, but the printing is not keywords array printing is not happening.

  ## DN [AMBIT] bash,resource
  ##   hodeuiihoedu
  ##   AVAL:
  ##   + ooeueocu
  ## END OF DN [AMBIT]
AdminBee
  • 21,637
  • 21
  • 47
  • 71
Vera
  • 1,173
  • 4
  • 17
  • 2
    Hello, and welcome to U&L! What have you tried? How did that differ from expectations or intent? – DopeGhoti Feb 02 '23 at 08:57
  • 2
    Is this homework? We had a very similar set of questions just last week. For example, [Printing sectioned parts in terminal](https://unix.stackexchange.com/questions/733772/printing-sectioned-parts-in-terminal) – roaima Feb 02 '23 at 09:05
  • Just something which is useful to us. – Vera Feb 02 '23 at 09:09
  • Yes, but this is about doing a test on such files that all begin and end sections are in order. – Vera Feb 02 '23 at 09:36
  • How can I change this to store an arbitrary number of keywords? – Vera Feb 03 '23 at 03:55
  • Why do you need to parse the keywords, they don't seem to be relevant to "ensure that the file contains the corresponding closing part to the definition"? – nohillside Feb 03 '23 at 15:48
  • They are not relevant as you say, but they may be present in the begin part. – Vera Feb 04 '23 at 05:31

1 Answers1

0

To read an arbitrary number of keywords, change the regex to find a pattern of any text followed by optional comma. The complete list of keywords will be captured into a single variable. Then read the comma-separated list into an array.

while read -r line; do
    if [[ $line =~ ^##\ ([A-Z]+)\ \[([A-Z]+)\]\ (.*[,]?)$ ]]; then
        title="${BASH_REMATCH[1]}"
        subtitle="${BASH_REMATCH[2]}"
        echo "Title: $title"
        echo "Subtitle: $subtitle"

        # read keyword list into array
        IFS=',' read -ra keywords <<< "${BASH_REMATCH[3]}"
        i=0
        for kw in "${keywords[@]}"; do
            echo "Keyword$((i+=1)): $kw"
        done
        echo
    fi
done << EOF

## FAML [ASMB] keyword1,keyword2  

## Some text
## Description
## END OF FAML [ASMB]

Some Code

## More text

## FALUN [GONG] keyword1,keyword2,keyword3,keyword4

## Some text
## Description
## END OF FALUN [GONG]
EOF

Output:

Title: FAML
Subtitle: ASMB
Keyword1: keyword1
Keyword2: keyword2

Title: FALUN
Subtitle: GONG
Keyword1: keyword1
Keyword2: keyword2
Keyword3: keyword3
Keyword4: keyword4

floydn
  • 111
  • 4