Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning bash scripting and I am trying to understand how regex works with bash. In my simple program, I have a file of various different lines but want to match a simple int pair with the format of the two numbers separated by a comma and a space as so: 123, 123. I also want to match the line with groups so I can easily extract the first and last numbers from the line. But when I pass a sample file to my program, it doesn't match any of the lines that I expect.

What I have tried:

Bash
#!/bin/bash

re="([0-9]+)(, )([0=9]+)"

echo $1
file1=$1

while IFS= read -r line; do
        echo "The line is: $line"
        if [[ $line =~ $re ]]; then
               echo ${BASH_REMATCH[0]};
               echo ${BASH_REMATCH[1]};
               echo ${BASH_REMATCH[2]};
        fi
done
Posted
Updated 29-Sep-16 7:53am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900