Solved

Using awk to filter and print matching regular expression

Posted on 2009-06-29
5
895 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Fine Tune your automatic Updates for Ubuntu / Debian
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
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.

831 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