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}:digi t:{2}]/ {print}'
----code end----
but I only want to print the matching pattern like shown in my grep example.
Thanks for any help.
----code start----
GetDate='Blabla - Blala (06.29.09)'
echo $GetDate | awk '/[:digit:{2}.{1}:digit:{2
----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}'
echo $GetDate | awk 'match($0,/[[:digit:]][[:d igit:]]\.[ [:digit:]] [[:digit:] ]\.[[:digi t:]][[:dig it:]]/){pr int substr($0,RSTART,RLENGTH)} '
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
-----
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 ""
}
}
}
}'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ozo,
That did the trick, thanks. It looks like it doesn't supports the newer posix character sets [[:digit:]].
That did the trick, thanks. It looks like it doesn't supports the newer posix character sets [[:digit:]].