你或許應該這樣做在Perl/Python的,但我很無聊,爲什麼不:
#!/usr/bin/env bash
filename=$1
#read input file and store into associative array
declare -A arr
while IFS=',' read date value string ; do
#Change time from 10h00h00 to 10:00:00
dt=$(sed s'/\([0-9]\+\)h/\1:/g' <<< "$date")
#Get rid of leading spaces
string=$(tr -d ' ' <<< "$string")
value=$(tr -d ' ' <<< "$value")
#Convert datetime to unix epoch
epoch=$(date --date="$dt" +%s)
#echo "$epoch,$string"
#Create unique key for array
key="$epoch,$string"
#Check if key already exists if so compare
#values and keep the highest
ov=-10000
[ ${arr["$key"]+abc} ] && ov=${arr["$key"]}
if [[ "$value" -gt "$ov" ]]; then
arr["$key"]="$value"
fi
done < "$filename"
#Display output, sort on epoch and then remove from output
for i in ${!arr[@]}; do
#Split key into epoch and string
epoch=$(awk -F, '{print $1}' <<< $i)
string=$(awk -F, '{print $2}' <<< $i)
#Convert date back to long format
dt=$(date [email protected]"$epoch" '+%h %-d %Y %Hh%Mh%S')
echo "$epoch $dt, ${arr[$i]} $string"
done | sort -h | sed 's/^[0-9]\+ //'
輸入文件(數據文件):
Mar 6 2014 17h29h43, -55, xx:xx:xx:xx:xx
Mar 6 2014 17h29h43, -38, xx:xx:xx:xx:xx
Mar 6 2014 17h29h44, -60, yy:yy:yy:yy:yy
Mar 9 2014 07h29h44, -6, 11:22:33:44:55
Mar 9 2014 07h29h44, 6, 22:33:44:55:66
Mar 9 2014 07h29h44, 3, 22:33:44:55:66
Mar 1 2014 14h33h04, -60, anythingreally
輸出:
Mar 1 2014 14h33h04, -60 anythingreally
Mar 6 2014 17h29h43, -38 xx:xx:xx:xx:xx
Mar 6 2014 17h29h44, -60 yy:yy:yy:yy:yy
Mar 9 2014 07h29h44, -6 11:22:33:44:55
Mar 9 2014 07h29h44, 6 22:33:44:55:66
歡迎[所以]。請提供您嘗試的解決方案/代碼,並告訴我們出了什麼問題。這樣你會找到更好的答案和幫助。 –
這是必須在bash中完成的原因嗎?您應該使用awk或perl來執行此類處理。 –
查看['csvfix'](https://code.google.com/p/csvfix/)是否可以提供幫助;它是用於處理CSV文件的非常強大的工具。或者,這看起來像是'awk'的工作。 –