×
UNIX.COM Login
Username:
Password:  
Show Password






👤


UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

awk question

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2017
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
awk question

Trying to parse data with three columns. The 2nd and 3rd columns are not constant and there is a data stamp. Tried a few iterations of awk but can't seem to get the desired output.

This is the format:


Code:
entry1 2017-12-12 some_text
entry1 2017-12-13 some_text
entry1 2017-12-18 some_text
entry2 2017-12-10 some_text
entry3 2017-12-08 some_text
entry3 2017-12-04 some_text

This is the desired output (trying to get one entry with the latest date):


Code:
entry1 2017-12-18 some_text
entry2 2017-12-10 some_text
entry3 2017-12-08 some_text

Any help is appreciated.
Sponsored Links
    #2  
Old 12-28-2017
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 15 July 2018, 8:03 PM EDT
Location: Galactic Empire
Posts: 3,671
Thanks: 257
Thanked 1,331 Times in 1,247 Posts
For unsorted date input, try:-


Code:
sort -k2.1,2.5 -k2.6,2.8 -k2.9,2.11 file | awk 'p&&p!=$1{print v}{p=$1;v=$0}END{print v}'

Sponsored Links
    #3  
Old 12-28-2017
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 18 May 2018, 12:29 PM EDT
Posts: 112
Thanks: 57
Thanked 2 Times in 1 Post
thank you Yoda, I've never used multiple instances of sort. Happy new year Cheers!
    #4  
Old 12-28-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 July 2018, 7:55 AM EDT
Location: Aachen, Germany
Posts: 13,059
Thanks: 447
Thanked 4,008 Times in 3,685 Posts
Hmmm - those are not multiple instances of sort but just three sort key definitions - which are not needed, or even can spoil the result. Try simply

Code:
sort file | awk 'p&&p!=$1{print v}{p=$1;v=$0}END{print v}'

Sponsored Links
👤 Login to reply

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
awk question bombcan Shell Programming and Scripting 3 09-04-2014 02:59 PM
awk question : system output to awk variable. rveri Shell Programming and Scripting 5 04-23-2013 01:29 AM
Another AWK question rk4k Shell Programming and Scripting 7 01-19-2010 12:01 AM
Basic awk question...getting awk to act on $1 of the command itself JasonHamm UNIX for Dummies Questions & Answers 2 04-02-2009 08:52 AM
question on awk xinoo Shell Programming and Scripting 2 02-03-2009 11:06 AM



All times are GMT -4. The time now is 08:46 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.