1

Possible Duplicate:
Split string based on delimiter in bash?

I have a bunch of files named test<numbers or letters>.<number>.out so like test1.1024.out or test2.2.out. Is there some way I can use a regular expression like ^test.*?..(.*).out$ to parse out the middle number on each file and then be able to access the group?

Community
  • 1
  • 1
Ian Burris
  • 6,325
  • 21
  • 59
  • 80

3 Answers3

2
for f in test*.out; do
  number=${f#test*.}
  number=${number%.*}
  echo $f has middle number $number
done
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
2
sed -r 's/^test[[:alnum:]]*\.([[:digit:]]+)\.out$/\1/'

Alternatively (A shorter version of the BASH for loop):

awk -F '.' '{print $2}'
Swiss
  • 5,556
  • 1
  • 28
  • 42
2

Depending on the version of Bash this could work too:

 test=test1.1024.out
 if [[ $test =~ ^test[A-Za-z0-9]+\.([0-9]+)\.out$ ]]; then
      echo ${BASH_REMATCH[1]}
 fi
Niall Byrne
  • 2,448
  • 1
  • 17
  • 18