shell - finding maximum from partial string -
i have list first 6 digit date in format yyyymmdd. next 4 digits part of timestamp. want select numbers maximum timestamp day.
20160905092900 20160905212900 20160906092900 20160906213000 20160907093000 20160907213000 20160908093000 20160908213000 20160910093000 20160910213100 20160911093100 20160911213100 20160912093100
means above list output should give below list.
20160905212900 20160906213000 20160907213000 20160908213000 20160910213100 20160911213100 20160912093100
you can use awk:
awk '{ dt = substr($0, 1, 8) ts = substr($0, 9, 12) } ts > max[dt] { max[dt] = ts rec[dt] = $0 } end { (i in rec) print rec[i] }' file 20160905212900 20160906213000 20160907213000 20160908213000 20160910213100 20160911213100 20160912093100
we using associative array max
uses first 8 characters key , next 4 characters value. array being used store max timestamp value given date. array rec
used store full line date when encounter timestamp value greater stored value in max
array.
Comments
Post a Comment