1

I made this batch script that is parsing a file and displaying my values if found :

#!/bin/bash
myFile="my-services.log"
while read p; do
    re="(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3}).*:(\d*)"
    if [[ $p =~ $re ]]; then 
        echo ${BASH_REMATCH[0]} 
        echo ${BASH_REMATCH[1]}
        echo ${BASH_REMATCH[2]} 
    fi
done <$myFile

My log file contains things like this

2017-03-24 07:51:43,368 my log id :469565

My script runs well, but doesnt match anything even with data that are matching my regex. I checked my regex with my file editor and it is finding occurrences, so it is strange.

Please note that I was highly insprired from Regular Expression in Bash Script and Multiple matches in a string using regex in bash

Community
  • 1
  • 1
Nicolas D
  • 1,182
  • 18
  • 40

1 Answers1

2

Bash does not support \d, \s.

[STEP 100] $ echo $BASH_VERSION
4.4.12(3)-release
[STEP 101] $ d4='[[:digit:]]{4}'
[STEP 102] $ d2='[[:digit:]]{2}'
[STEP 103] $ d3='[[:digit:]]{3}'
[STEP 104] $ ds='[[:digit:]]+'
[STEP 105] $ re="^($d4-$d2-$d2 $d2:$d2:$d2,$d3) .*:($ds)\$"
[STEP 106] $ str='2017-03-24 07:51:43,368 my log id :469565'
[STEP 107] $ [[ $str =~ $re ]]
[STEP 108] $ echo $?
0
[STEP 109] $ echo ${BASH_REMATCH[1]}
2017-03-24 07:51:43,368
[STEP 110] $ echo ${BASH_REMATCH[2]}
469565
[STEP 111] $
pynexj
  • 19,215
  • 5
  • 38
  • 56