Link to home
Start Free TrialLog in
Avatar of illtbagu
illtbagu

asked on

Using awk to filter and print matching regular expression

I am trying to figure out how to use awk to only print matching regular expression, see grep example in code snippet listed below. Something like this
----code start----
GetDate='Blabla - Blala (06.29.09)'
echo $GetDate | awk '/[:digit:{2}.{1}:digit:{2}.{1}:digit:{2}]/ {print}'
----code end----
but I only want to print the matching pattern like shown in my grep example.

Thanks for any help.
GetDate='Blabla - Blala (06.29.09)'
echo $GetDate | grep -E -o '[[:digit:]]{2}.{1}[[:digit:]]{2}.{1}[[:digit:]]{2}'

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

echo $GetDate | awk 'match($0,/[[:digit:]][[:digit:]]\.[[:digit:]][[:digit:]]\.[[:digit:]][[:digit:]]/){print substr($0,RSTART,RLENGTH)}'
Avatar of illtbagu
illtbagu

ASKER

For whatever reason that didn't work.
-----
awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan
compiled limits:
max NF             32767
sprintf buffer      1020

#!/bin/bash
GetDate='Blabla (06.29.00) - Blala (06.29.09)'
echo $GetDate | awk '{ 
 for(i=1;i<=NF;i++){
    f=0
    gsub(/\(|\)/,"",$i)
    m=split($i,t,".")
    if (m==3) {
        for(o=1;o<=m;o++){
            if(t[o]+0!=t[o]){ f=1 ;break}        
        }
        if(!f){ 
            for(o=1;o<=m;o++) {
                if(o==m) printf t[o]
                else printf t[o]"." 
            }
            print ""
        }    
    }
 }
}'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ozo,
That did the trick, thanks. It looks like it doesn't supports the newer posix character sets [[:digit:]].