1

I have a text file. I want to get lines starting with specific format. I just want to get lines that have x/x/x format. x is a number. But this regex is not working. It is always giving no match :

while read line           
do           
    regex="\d+\/\d+\/\d+"
    if [[ ${line} =~  ${regex} ]]; then 

        echo ${line}

    else

        echo "no match : ${line}"

    fi    
done <${textFileName}

File is :

enter image description here

1 Answer 1

1

Don't use bash if you can use a better tool:

grep -E '^[[:digit:]]+/[[:digit:]]+/[[:digit:]]+' "${textFileName}"

But if you have to use bash:

while IFS= read -r line
do
  if [[ "$line}" =~ ^[[:digit:]]+/[[:digit:]]+/[[:digit:]]+ ]]; then
     echo -- "$line"
  else
    echo "no match: $line"
  fi
done < "$textFileName"

\d is not valid regex(3).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi i applied your code. But didnt work. while IFS= read -r line do if [[ "${line}" =~ '[[:digit:]]+/[[:digit:]]+/[[:digit:]]+' ]]; then echo -- "X:$line" else echo "no match: $line" fi done < "$textFileName"
Don't quote the regex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.