Unix/Linux Go Back    


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

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-28-2017   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
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 Unix and Linux 12-28-2017   -   Original Discussion by jimmyf
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 10 May 2018, 11:06 AM EDT
Location: Galactic Empire
Posts: 3,670
Thanks: 257
Thanked 1,330 Times in 1,246 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 Unix and Linux 12-28-2017   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
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 Unix and Linux 12-28-2017   -   Original Discussion by jimmyf
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 26 May 2018, 5:24 PM EDT
Location: Aachen, Germany
Posts: 12,756
Thanks: 417
Thanked 3,923 Times in 3,607 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
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 05:53 AM.