I have the following output example:
[OK] AAA.BBBBBB
aaabbbcccdddfffed
asdadadadadadsada
[OK] CCC.KKKKKKK
some text here
[OK] OKO.II
and I want to parse it via a this regex \[OK\](\s+\w+)\.(\w+)\n([^\[]+)
but when I am trying to create my shell script which looks like this:
#!/bin/bash
# Define the text to parse
text="[OK] AAA.BBBBBB
aaabbbcccdddfffed
asdadadadadadsada
[OK] CCC.KKKKKKK
some text here
[OK] OKO.II"
# Create an empty list to hold the group lists
# Loop through the text and extract all matches
regex_pattern="\[OK\](\s+\w+)\.(\w+)\n([^\[]+)"
while [[ $text =~ $regex_pattern ]]; do
# Create a list to hold the current groups
echo "Matched_1: ${BASH_REMATCH[1]}"
echo "Matched_2: ${BASH_REMATCH[2]}"
echo "Matched_3: ${BASH_REMATCH[3]}"
echo "-------------------"
done
Is not going to output anything...

bashis notPerlorPCRE.\wshould be replaced by[a-zA-Z_]and I doubtbashcan do multiline regex with\n\n(you may introduce a literal line break in the pattern) and\w(replace with[[:alnum:]_]) /\s(replace with[[:space:]]) are minor issues, you just can't do multiple matching easily the way you did. Also, consider quoting the$textvariable.grep: character class syntax is [[:space:]], not [:space:]. Also if this is not possible like this... then how can I obtain an multiple matching in bash?bash, see my answer below