I have a text file with various entries in it. Each entry is ended with line containing all asterisks.
I\'d like to use shell commands to parse this file and assign each
I'm surprised to not see a native bash solution here. Yes, bash has regular expressions. You can find plenty of random documentation online, particularly if you include "bash_rematch" in your query, or just look at the man pages. Here's a silly example, taken from here and slightly modified, which prints the whole match, and each of the captured matches, for a regular expression.
if [[ $str =~ $regex ]]; then
echo "$str matches"
echo "matching substring: ${BASH_REMATCH[0]}"
i=1
n=${#BASH_REMATCH[*]}
while [[ $i -lt $n ]]
do
echo " capture[$i]: ${BASH_REMATCH[$i]}"
let i++
done
else
echo "$str does not match"
fi
The important bit is that the extended test [[ ... ]]
using its regex comparision =~
stores the entire match in ${BASH_REMATCH[0]}
and the captured matches in ${BASH_REMATCH[i]}
.