【问题标题】:Search all records using bash within some timeframe在某个时间范围内使用 bash 搜索所有记录
【发布时间】:2017-01-13 12:16:41
【问题描述】:

我有一个 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 日的所有记录

【问题讨论】:

  • bash 不是这个工具,使用awk
  • 或者尝试下载使用dategrep
  • 注意,你可以通过说while IFS=, read -r col1 col2 col3 col4 _直接得到$Timestamp;这会将第一个字段存储在$col1 中,第二个字段存储在$col2 中,依此类推。
  • @Inian,你能提供一些关于 awk 的代码示例吗?
  • @VasiliyVegas:这里有很多类似的答案,尝试一下,在这里发表你的努力。

标签: bash csv search awk time


【解决方案1】:

使用 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,

【讨论】:

  • 太棒了!感谢您的帮助!
  • 另一个问题 - 你能重写你的 awk 字符串来把所有输出放到某个文件或变量中吗?因为我不太明白如何在 awk 中执行此操作。
  • 你只需要在最后像这样重定向 o/p inputfile >outputfile
  • 谢谢!它也有帮助!
猜你喜欢
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 2020-10-09
相关资源
最近更新 更多