LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 01-04-2011, 01:43 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
awk: awking the right separator with slashes, colons, commas (date time and data)


I wrote a hack script that outputs the following every so often:

Code:
01/04/11 10:33:02: 97,1413,1447,2860
I must leave the data format the same --but I want a special number from it. In this case it's 97 and it's always going to be the first in the 4 columns of comma delimited items. I can extract with this:
Code:
cat datafile | awk -F" " {'print $3'} | awk -F"," {'print $1'}
But that's really sloppy. Can someone point out a better way of doing this (with awk) and tell me why?

Thanks a bunch!

-zeek
 
Old 01-04-2011, 01:47 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Try this:
Code:
awk -F"[ ,]" '{ print $3 }' datafile
You can use multiple separators (space and comma in this example), they do need to be between square brackets.

Hope this helps.
 
1 members found this post helpful.
Old 01-04-2011, 01:53 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or eventually
Code:
cat datafile | awk '{sub(/,.*/,"",$NF); print $NF}'
or $3 in place of $NF if the lines contain more fields.
 
Old 01-04-2011, 01:58 PM   #4
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
druuna: that multiple separators trick is nice. Thanks.

colucix: Thanks for that suggestion. I'll check it out too.
 
  


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
How to search a string having spaces, slashes, colons in vi editor RajRed Linux - Newbie 7 02-02-2010 06:11 PM
print FS (field separator) in awk wakatana Programming 5 11-05-2009 09:17 AM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 08:37 AM
awk record separator question johnpaulodonnell Linux - Newbie 2 07-30-2007 10:35 AM
My field separator changes when using awk Helene Programming 3 05-01-2004 09:10 AM


All times are GMT -5. The time now is 07:59 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