The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular expression query in AWK omprasad Shell Programming and Scripting 10 01-11-2008 02:26 PM
Regular expression maxvirrozeito UNIX for Dummies Questions & Answers 1 12-14-2007 05:02 AM
What does the regular expression ['(^[^~]+~).*'] mean? anupamar High Level Programming 2 10-29-2007 01:41 AM
help in regular expression Rakesh Ranjan High Level Programming 5 10-25-2006 10:00 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 07:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-14-2006
Registered User
 

Join Date: Jun 2006
Posts: 7
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
Reply With Quote
Forum Sponsor
  #2  
Old 09-15-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
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
Reply With Quote
  #3  
Old 09-15-2006
Registered User
 

Join Date: Jun 2006
Posts: 7
Thumbs up

Thank Ygor....It worked.
It would be helpful if you could explain the split regular expression
Reply With Quote
  #4  
Old 09-15-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
See Regular Expression Operators
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0