考虑输入文件
1,10/22/2017,Scheduled
2,10/23/2017,Confimred
1,10/24/2017,NA
1,10/29/2017,Scheduled
3,11/1/2017,Scheduled
1,11/2/2017,Scheduled
如何通过提供日期范围作为输入来过滤第二列中的日期(在范围内)?
答案1
这个片段:
# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pl " Input data file $FILE:"
head data1
# start="10/29/2017" end="11/2/2017"
START="10/29/2017"
END="11/2/2017"
pl " Results, from $START through $END:"
dateutils.dgrep -i "%m/%d/%Y" ">=$START" '&&' "<=$END" < data1
pl " Unsorted file, data2:"
head data2
pl " Results, from $START through $END, randomly organized file:"
dateutils.dgrep -i "%m/%d/%Y" ">=$START" '&&' "<=$END" < data2
产生:
-----
Input data file :
1,10/22/2017,Scheduled
2,10/23/2017,Confimred
1,10/24/2017,NA
1,10/29/2017,Scheduled
3,11/1/2017,Scheduled
1,11/2/2017,Scheduled
-----
Results, from 10/29/2017 through 11/2/2017:
1,10/29/2017,Scheduled
3,11/1/2017,Scheduled
1,11/2/2017,Scheduled
-----
Unsorted file, data2:
1,10/22/2017,Scheduled
1,10/24/2017,NA
1,10/29/2017,Scheduled
1,11/2/2017,Scheduled
2,10/23/2017,Confimred
3,11/1/2017,Scheduled
-----
Results, from 10/29/2017 through 11/2/2017, randomly organized file:
1,10/29/2017,Scheduled
1,11/2/2017,Scheduled
3,11/1/2017,Scheduled
在这样的系统上:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
由于比较是对日期格式的数据进行算术运算,因此数据可以采用任何顺序。如果需要,可以对最终结果进行排序——请参阅 sort、msort、dsort。 dateutils 代码在许多存储库和 OSX 中(通过brew)都可用。
dateutils.dgrep 的一些详细信息:
dateutils.dgrep Grep standard input for lines that match EXPRESSION. (man)
Path : /usr/bin/dateutils.dgrep
Package : dateutils
Home : http://www.fresse.org/dateutils
Version : 0.3.1
Type : ELF64-bitLSBsharedobject,x86-64,version1(S ...)
Help : probably available with -h,--help
Home : https://github.com/hroptatyr/dateutils (doc)
最美好的祝愿...干杯,drl
答案2
使用awk
和调用shelldate
命令从管道使用 getline:
awk -v start="$start" -v end="$end" -F, '
BEGIN{srt="date -d"start" +%s"; srt|getline start; close(srt);
ed="date -d"end" +%s"; ed|getline end; close(ed) }
{ bkp=$0; epoch="date -d"$2" +%s";epoch |getline $2;close(epoch)};
($2>=start && $2<=end){print bkp}' infile
对于以下输入:
1,10/22/2017,Scheduled
1,10/24/2017,NA
1,10/24/2017,NA,NA
1,10/29/2017,Scheduled
3,11/1/2017,Scheduled
1,11/2/2017,NA
5,9/30/2017,Confirmed
6,10/1/2017,Scheduled
使用start='10/24/2017'
和end='11/1/2017'
,结果是:
1,10/24/2017,NA
1,10/24/2017,NA,NA
1,10/29/2017,Scheduled
3,11/1/2017,Scheduled