我有一个csv文件,其数据如下:
67940,"Alpha",ISS3425345,12/9/2014 21:12,
69542,"Beta",ISS03425324,1/16/2015 11:56,
69761,"Gamma",ISS02345,1/22/2015 12:54,
用逗号作为分隔符。第4个字段是记录创建的时间戳。我需要编写一个脚本来输入作为参数的开始和结束时间帧来搜索这个时间范围内的所有记录。
我目前的进展:
#!/bin/bash
SearchStart=$1
SearchEnd=$2
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -f "$DIR/Output_data.csv" ]; then
touch $DIR/Output_data.csv
fi
while IFS= read -r current_escalation; do
Timestamp=$( echo $current_escalation | cut -d',' -f4 )
(Here is some script to search records from SearchStart to SearchEnd)
done <$DIR/input_data.csv
我需要脚本,它会在时间范围内返回所有结果。例如,开始日期为2014年9月15日,截止日期为2015年10月20日。我需要2014年9月5日至2015年10月20日的所有记录
答案 0 :(得分:2)
使用 GNU awk
单线
awk -F, -vstartd="12/9/2014 22:00:00" -vendd="1/22/2015 10:00:00" 'function df(dt, d){split(dt,d,/[/: ]/); return mktime(d[3]" "d[1]" "d[2]" "d[4]" "d[5]" "0)}{s=df($4)} s >=df(startd) && s<=df(endd)' file
<强>解释强>
# Set field separator comma (-F,)
# Set startd and endd variable, in your case within bash script
# you can do -vstartd="$1" and -vendd="$2" for searching
awk -F, -vstartd="12/9/2014 22:00:00" -vendd="1/22/2015 10:00:00" '
# function which takes input in
# month-day-year hour:minute format and
# returns Unix time
function df(dt, d)
{
split(dt,d,/[/: ]/)
return mktime(d[3]" "d[1]" "d[2]" "d[4]" "d[5]" "0)
}
{
# we do not want to call function twice for below statement
# so assigning converted value to variable s
s=df($4)
}
# So if variable s is greater than or equal to start datetime and
# less than or equal to enddatetime
# we get boolean true ( default operation print $0 takes place),
# hence print current record/row
s >=df(startd) && s<=df(endd)
' file # Input file
<强>输入强>
$ cat f
67940,"Alpha",ISS3425345,12/9/2014 21:12,
69542,"Beta",ISS03425324,1/16/2015 11:56,
69761,"Gamma",ISS02345,1/22/2015 12:54,
<强>输出强>
$ awk -F, -vstartd="12/9/2014 22:00:00" -vendd="1/22/2015 10:00:00" 'function df(dt, d){split(dt,d,/[/: ]/); return mktime(d[3]" "d[1]" "d[2]" "d[4]" "d[5]" "0)}{s=df($4)} s >=df(startd) && s<=df(endd)' f
69542,"Beta",ISS03425324,1/16/2015 11:56,