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 2 Weeks Ago   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 28 December 2017, 12:59 PM EST
Posts: 108
Thanks: 54
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 2 Weeks Ago   -   Original Discussion by jimmyf
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 January 2018, 2:27 PM EST
Location: Galactic Empire
Posts: 3,630
Thanks: 256
Thanked 1,323 Times in 1,240 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 2 Weeks Ago   -   Original Discussion by jimmyf
jimmyf's Unix or Linux Image
jimmyf jimmyf is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 28 December 2017, 12:59 PM EST
Posts: 108
Thanks: 54
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 2 Weeks Ago   -   Original Discussion by jimmyf
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 January 2018, 2:48 PM EST
Location: Aachen, Germany
Posts: 11,940
Thanks: 352
Thanked 3,678 Times in 3,376 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 03:59 PM
awk question : system output to awk variable. rveri Shell Programming and Scripting 5 04-23-2013 02:29 AM
Another AWK question rk4k Shell Programming and Scripting 7 01-19-2010 01: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 09:52 AM
question on awk xinoo Shell Programming and Scripting 2 02-03-2009 12:06 PM



All times are GMT -4. The time now is 04:14 PM.