bash sort - how do I sort using timestamp

后端 未结 2 678
南方客
南方客 2020-12-21 13:39

I need to sort a file using shell sort in linux. The sort needs to be based on timestamp values contained within each of file\'s rows. The timestamps are of irregular format

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 14:21

    The linux date command does a fine job of parsing dates like this, and it can translate them into more sortable things, like simple unix-time integers.

    Example:

    cat file | while read line; do
        datestring=$(sed -e 's/^.* t="\([^"]*\)".*$/\1/' <<<"$line")
        echo "$(date -d "$datestring" +%s) $line"
    done | sort -n
    

    then you could pass that through the appropriate cut invocation if you want that unix timestamp removed again.

提交回复
热议问题