0

I have a list of month, date, and timestamps in a file like this:

Jan19 03:05
Jan19 15:05
Jan20 03:05
Jan20 15:05
Jan21 03:05
Jan21 15:06
Jan22 03:05
Jan22 15:06
Dec25 15:05
Dec26 14:06
Dec27 15:06
Dec28 15:06
Dec29 14:05
Dec30 14:06
Dec31 15:06

I need to just get the most recent 30 entries. My code is:

cat file | sort -k1.1,1.3M -k1.4n -k2V 

This sort is sorting the Dec entries as more recent than the Jan. I think it's because 12 is bigger than 1 but is there a way to get Jan to come to the end of this file?

6
  • A much better solution is to use a sane date format in the first place. Removing the M option sorts alphabetically, which happens to do the right thing here.
    – tripleee
    Jan 23, 2020 at 18:44
  • 2
    Is January more recent because the entries are missing the year? Because if so, there's no robust way – your input file doesn't have enough information. Jan 23, 2020 at 18:50
  • 1
    Your data is ambiguous. If the January dates are supposed to be more recent than the December dates (because the former are from 2020 and the later from 2019), then the year should be in the data to begin with.
    – chepner
    Jan 23, 2020 at 18:50
  • @chepner Jinx ;) Jan 23, 2020 at 18:50
  • Yes. I've realized that this entire file needs the year. Otherwise even if I figure out this current issue, when December comes around, I will have a new issue. Thanks everyone.
    – Hua Cha
    Jan 23, 2020 at 18:51

2 Answers 2

0

Assuming the all dates in the list are within last one year from now, how about:

now=$(date "+%m%d %H:%M")               # current date and time
declare -A m2n=([Jan]="01" [Feb]="02" [Mar]="03" [Apr]="04"
    [May]="05" [Jun]="06" [Jul]="07" [Aug]="08"
    [Sep]="09" [Oct]="10" [Nov]="11" [Dec]="12"
)
while IFS= read -r line; do
    if [[ $line =~ (^[A-Z][a-z]{2})(.+) ]]; then
        datetime="${m2n[${BASH_REMATCH[1]}]}${BASH_REMATCH[2]}"
        if [[ $datetime > $now ]]; then
            datetime="0$datetime"       # previous year
        else
            datetime="1$datetime"       # this year
        fi
        printf "%s\t%s\n" "$datetime" "$line"
    else
        echo "$line"                    # does not match the expected format
    fi
done < file | sort | cut -f 2-
  • It compares each date string with current date/time in dictionary order.
  • If the latter is larger, the date is assumed to be this year and put "1" in the "year" field, else put "0" there.
  • Then prepend the generated date string in front of the original lines, sort, and finally remove the portion.
0

Just replace with regex to prepare the data for sorting with a value for Jan that will come after Dec:

# JanXY -> 13 XY JanXY ; DecXY -> 12 XY DecXY
sed 's/^Jan\([0-9][0-9]\)/13 \1 &/; s/^Dec\([0-9][0-9]\)/12 \1 &/' |
sort -n -k1 -k2 | cut -d' ' -f3-5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.