LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2014, 02:09 PM   #1
waynedailey1
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
awk to find string within a file and return the next field. ex. Date: 01/01/2001


Pretty green at this, appreciate any help.

I have a pile of reports each night, and need to extract a date from each of them. They are not uniform and date varies in position.

The following code works for me, but the date is not always in $9.

MyDate=$(gawk '/Date:/{if (NR<2) print $9}' $MyPrnFileName)


Thanks for your help!
 
Old 06-03-2014, 12:33 AM   #2
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,307
Blog Entries: 2

Rep: Reputation: Disabled
Does the date format remain constant?
 
Old 06-03-2014, 12:40 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I am not sure I follow the issue? The next field is obviously 10 so using the next field number is not hard.

Please explain further and supply examples so it is clearer.

Also, please use [code][/code] tags around code or data.
 
Old 06-03-2014, 01:45 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Quote:
Originally Posted by waynedailey1 View Post
They are not uniform and date varies in position.
When faced with this I use match() with the optitional array specified, and build the regex to ensure known fields arrive in pre-determined array indices - i.e. the string "Date:" is say a[1], and the actual field I want is always a[2]. Works a treat.
 
Old 06-03-2014, 02:13 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
I would use perl and regex to catch it, but as it was told already we need more info about how those source files look like.
 
Old 06-03-2014, 05:35 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I'm with pan64 re using Perl; its very flexible.
IME, even with varying records, there's usually only a few variations; you just need to examine the records carefully.
 
Old 06-03-2014, 05:51 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
As I said, gawk has a builtin function with full regex support that'll do it - if the OP is comfortable in gawk, no need to go to another language.
I initially used match() on SNMP logs they are of varying layout, and it worked fine.
 
Old 06-03-2014, 07:56 AM   #8
waynedailey1
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
When faced with this I use match() with the optitional array specified, and build the regex to ensure known fields arrive in pre-determined array indices - i.e. the string "Date:" is say a[1], and the actual field I want is always a[2]. Works a treat.
Thanks syg00, sounds like what I need.

Can you give me an example of how this is done?
 
Old 06-03-2014, 08:03 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
String-Functions - includes examples in the "match()" section.
Very worthwhile to download the entire manual.

Have a go - if you run into problems, post your code and we maybe can give some pointers.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK: how to replace particular field or sub-string? cristalp Programming 8 06-15-2013 04:23 AM
[SOLVED] Using Awk to replace the 6th Field of a string metallica1973 Programming 4 08-06-2012 03:48 PM
[SOLVED] Awk script to look for string, show value in field 2, if not present print zero Perseus Programming 12 10-06-2011 04:40 AM
[SOLVED] awk - sum total if field = string schneidz Programming 12 03-20-2010 05:56 PM


All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration