Regular expression query in AWK | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Regular expression query in AWK

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-15-2006
omprasad omprasad is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 16 November 2006, 6:25 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Question Regular expression query in AWK

I have a varable(var1) in a AWK script that contain data in the following format

[timestamp] [priority] - [log message]

I need to extract timestamp,priority and log message.I can extract these by using split function but i don't want to use it, since i want to extract it in one go. I have some difficulties in doing it using Regular expression using a single statement. Can anybody help me on this
Sponsored Links
    #2  
Old 09-15-2006
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 111 Times in 98 Posts
Try...
Code:
awk 'BEGIN{
      var = "[timestamp] [priority] - [log message]"
      n = split(var, arr, /(^\[)|(\] -? ?\[)|(\]$)/)
      for (x=2; x<n; x++)
          printf "arr[%d]=%s\n", x, arr[x]
     }'

...result is...
Code:
arr[2]=timestamp
arr[3]=priority
arr[4]=log message

Sponsored Links
    #3  
Old 09-15-2006
omprasad omprasad is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 16 November 2006, 6:25 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL

Thank Ygor....It worked.
It would be helpful if you could explain the split regular expression
    #4  
Old 09-15-2006
Ygor's Avatar
Ygor Ygor is offline Forum Advisor  
Advisor
 
Join Date: Oct 2003
Last Activity: 2 May 2014, 7:14 AM EDT
Location: 54.23, -4.53
Posts: 1,801
Thanks: 1
Thanked 111 Times in 98 Posts
See Regular Expression Operators
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: How to read from a file, do regular expression and then replace the found regular expression jessy83 Programming 1 12-05-2011 03:10 PM
Integer expression expected: with regular expression ketkee1985 Shell Programming and Scripting 3 03-14-2011 02:49 PM
Query Regarding Regular Expression Shell_Learner Shell Programming and Scripting 7 03-24-2009 09:41 AM
Regular expression query in AWK omprasad Shell Programming and Scripting 10 01-11-2008 04:26 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM



All times are GMT -4. The time now is 11:06 PM.