Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I assign the match of my regular expression to a variable?

Tags:

regex

bash

shell

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 entry to a variable. How can I do this?

Here's an example input file:

***********
Field1
***********
Lorem ipsum
Data to match
***********
More data
Still more data
***********

Here is what my solution looks like so far:

#!/bin/bash
for error in `python example.py | sed -n '/.*/,/^\**$/p'`
do
    echo -e $error
    echo -e "\n"
done

However, this just assigns each word in the matched text to $error, rather than a whole block.

like image 584
samoz Avatar asked Aug 07 '09 21:08

samoz


1 Answers

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]}.

like image 177
Cascabel Avatar answered Oct 08 '22 14:10

Cascabel