Regex in a bash scipt -


i've got following text file contains:

 12.3-456, test  test test test 

if line contains xx.x-xxx, want print line out. (x's numbers)

i think have correct regex , have tested here: http://regexr.com/3clu3

i have used in bash script line containing text not printed out. have messed up?

#!/bin/bash while ifs='' read -r line || [[ -n "$line" ]];     if  [[ $line =~ /\d\d.\d-\d\d\d,/g ]];             echo $line     fi done < input.txt 

you need use [0-9] instead of \d in bash regex. no regex delimiters necessary, , global flag not necessary either. also, can contract bit using limiting quantifiers (like {3} match 3 occurrences of pattern next it). besides, dot matches character in regex, need escape if want match literal dot symbol.

use

regex="[0-9]{2}\.[0-9]-[0-9]{3}," if  [[ $line =~ $regex ]] ... 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -