Sort Xeggex orders by date and time with AWK

This article is considering formatting date and time and sorting by date and time.

My example orders look like this

head xeggex-orders-overall.txt 
Time    Market    Side    Price    Quantity    Total w/ Fee    Discounts Fee (XPE)
1/10/2024, 10:30:43 AM    XTE/USDT    Buy    0.0000001515    10500100    1.5939466803    0
1/10/2024, 10:30:43 AM    XTE/USDT    Buy    0.00000017    20300800    3.458038272    0
1/10/2024, 5:22:02 PM    XTE/USDT    Sell    0.00000041    4500500    1.84151459    0
1/10/2024, 5:36:33 PM    XTE/USDT    Sell    0.00000044    12559344    5.51505913728    0
1/10/2024, 5:36:33 PM    XTE/USDT    Sell    0.00000043    2800200    1.201677828    0
1/10/2024, 5:41:52 PM    XTE/USDT    Sell    0.00000044    340756    0.14963277472    0
1/10/2024, 8:26:29 PM    XTE/USDT    Sell    0.00000046    8700100    3.994041908    0
1/11/2024, 1:10:37 PM    MAXE/USDT    Sell    0.00207    1900    3.925134    0
1/11/2024, 1:51:38 AM    LNCR/USDT    Sell    0.000675    1500    1.010475    0

Date and time has wrong format not supported by LibreOffice Calc and linux sort, and I cannot sort that. 

1/10/2024 - this is January 1, 2024. And linux utility "date" can only understand 01/10/2024, so need to add leading zeroes to months and days.

1:10:37 PM - time also lacks leading zeroes.

After formatting date and time I will convert it to timestamp and then sort as a numbers. And then I will remove timestamps

 

head -1 xeggex-orders-overall.txt > xeggex-orders-normalized.txt; sed 1d xeggex-orders-overall.txt | sed -E -e 's/([0-9]*)\/([0-9]*)\/([0-9]*), ([^M]*M)/\3-\1-\2 \4/' | sed -E -e 's/-([0-9])-/-0\1-/g' -e 's/-([0-9]) /-0\1 /g' | awk -F'\t' '{system("echo -n `date -d \""$1"\" +%s`\"\t\"`date -d \""$1"\" +\"%Y-%m-%d %H:%M:%S\"`"); for (i=2;i<=NF;i++) {printf "%s%s","\t",$i;};printf "%s","\n";}' | sort -n | cut --complement -f1 >> xeggex-orders-normalized.txt

tail xeggex-orders-normalized.txt 
2024-05-12 09:05:36    MAXE/USDT    Buy    0.0000651    2773.62    0.180923787324    0
2024-05-12 09:07:35    MAXE/USDT    Buy    0.0000651    285.2957    0.01860989557014    0
2024-05-12 09:08:42    MAXE/USDT    Buy    0.0000651    293.8702    0.01916921192004    0
2024-05-12 09:08:47    MAXE/USDT    Buy    0.0000651    293.2149    0.01912646656998    0
2024-05-12 09:24:23    MAXE/USDT    Buy    0.0000651    988.44    0.064476138888    0
2024-05-12 09:44:29    MAXE/USDT    Buy    0.0000651    250.7449    0.01635613997598    0
2024-05-12 10:28:32    MAXE/USDT    Buy    0.0000651    0.0011    0.00000007175322    0
2024-05-12 10:31:03    MAXE/USDT    Buy    0.0000651    2136.62    0.139372149924    0
2024-05-12 10:33:32    MAXE/USDT    Buy    0.0000651    9384.43    0.612148245786    0
2024-05-12 11:07:04    MAXE/USDT    Buy    0.0000651    888.32    0.057945291264    0

Date is well formatted, orders are sorted.
 

Section
Category