Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using awk to filter and print matching regular expression

Posted on 2009-06-29
5
Medium Priority
?
926 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 85

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 85

Accepted Solution

by:
ozo earned 840 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

972 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