Solved

Using awk to filter and print matching regular expression

Posted on 2009-06-29
5
911 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:illtbagu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24741202
echo $GetDate | awk 'match($0,/[[:digit:]][[:digit:]]\.[[:digit:]][[:digit:]]\.[[:digit:]][[:digit:]]/){print substr($0,RSTART,RLENGTH)}'
0
 
LVL 1

Author Comment

by:illtbagu
ID: 24741236
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
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24741650

#!/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

0
 
LVL 84

Accepted Solution

by:
ozo earned 210 total points
ID: 24741991
Did you get an error?
If mawk 1.3.3 does not support [[:digit:]] you might instead use
GetDate='Blabla - Blala (06.29.09)'
echo $GetDate | awk 'match($0,/[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]/){print substr($0,RSTART,RLENGTH)}'
0
 
LVL 1

Author Closing Comment

by:illtbagu
ID: 31598170
ozo,
That did the trick, thanks. It looks like it doesn't supports the newer posix character sets [[:digit:]].
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question