입력 파일 고려
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
및 호출date
파이프에서 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