如何从连续的 yyyy-mm-dd 格式的报告中提取详细信息,直到在 unix 中找到下一个 yyyy-mm-dd 条目
How to extract details from report having date in yyyy-mm-dd format in a row untill next yyyy-mm-dd entry founds in unix
我有一份报告(report.log),其中有这样的条目
Following are access details for date: 2015-02-18
---------------------------------------------------
details1
details2
....
....
Following are access details for date: 2015-02-19
----------------------------------------------------
line1
line2
....
....
Following are access details for date: 2015-02-20
-------------------------------------------------------
line1
line2
....
....
我想编写一个脚本,要求以 yyyy-mm-dd 格式输入单个日期(此输出应为以下是日期的访问详细信息:yyyy-mm-dd 直到并排除新输入行"following are access details for date") 或从 2014-02-09 到 2014-02-20 的范围(此输出应为以下是日期的访问详细信息:2014-02-09 到并包括 2014- 范围内的第二个日期02-20),这意味着输出日期范围内从第一个日期到最后一个日期的所有条目。
另请注意以下前后的新行:报告中的...行
这是一个使用 d1=$(date -d "somedate" +%s)
将秒转换为纪元的简单示例。然后第二天 d2=$((d1 + 86400))
。这使您可以在文件中找到包含第一个日期的行并读取所有行,直到找到第二天。如果您有任何问题,请告诉我:
#!/bin/sh
dt1=$(date -d "" +%s) # date provided by input
dt1f=$(date -d @$dt1 +%F) # date1 formatted in YYYY-MM-DD
dt2=$((dt1 + 86400)) # add one day to it
dt2f=$(date -d @$dt2 +%F) # date2 formatted in YYYY-MM-DD
ifn=${2:-loghdg.txt} # input file
# printf "\nDate Range: %s - %s\n\n" "$(date -d @$dt1 +%F)" "$(date -d @$dt2 +%F)"
printf "\nDate Range: %s - %s\n\n" "$dt1f" "$dt2f"
# read each line in file
while read -r line; do
arr=( $line ) # read line into array
for i in ${arr[@]}; do # for each word in line
[ $i == $dt1f ] && found=1 # if date1 found, print lines
[ $i == $dt2f ] && found=0 # if date2 found, stop printing
done
[ "$found" == "1" ] && printf " %s\n" "$line"
done <"$ifn"
输入文件
$ cat loghdg.txt
Following are access details for date: 2015-02-18
details1
details2
....
....
Following are access details for date: 2015-02-19
line1
line2
....
....
Following are access details for date: 2015-02-20
line1
line2
....
....
output/use
$ sh date-day-diff.sh 2015-02-18
Date Range: 2015-02-18 - 2015-02-19
Following are access details for date: 2015-02-18
details1
details2
....
....
接受 start
和 end
日期的修改相当简单。
修改为接受开始+结束日期
dt1=$(date -d "" +%s) # start date provided by input
dt1f=$(date -d @$dt1 +%F) # date1 formatted in YYYY-MM-DD
dt2=$(date -d "" +%s) # end date provided by input
dt2f=$(date -d @$dt2 +%F) # date2 formatted in YYYY-MM-DD
ifn=${3:-loghdg.txt} # input file
新输出
$ sh date-day-diff.sh 2015-02-18 2015-02-20
Date Range: 2015-02-18 - 2015-02-20
Following are access details for date: 2015-02-18
details1
details2
....
....
Following are access details for date: 2015-02-19
line1
line2
....
....
我有一份报告(report.log),其中有这样的条目
Following are access details for date: 2015-02-18
---------------------------------------------------
details1
details2
....
....
Following are access details for date: 2015-02-19
----------------------------------------------------
line1
line2
....
....
Following are access details for date: 2015-02-20
-------------------------------------------------------
line1
line2
....
....
我想编写一个脚本,要求以 yyyy-mm-dd 格式输入单个日期(此输出应为以下是日期的访问详细信息:yyyy-mm-dd 直到并排除新输入行"following are access details for date") 或从 2014-02-09 到 2014-02-20 的范围(此输出应为以下是日期的访问详细信息:2014-02-09 到并包括 2014- 范围内的第二个日期02-20),这意味着输出日期范围内从第一个日期到最后一个日期的所有条目。
另请注意以下前后的新行:报告中的...行
这是一个使用 d1=$(date -d "somedate" +%s)
将秒转换为纪元的简单示例。然后第二天 d2=$((d1 + 86400))
。这使您可以在文件中找到包含第一个日期的行并读取所有行,直到找到第二天。如果您有任何问题,请告诉我:
#!/bin/sh
dt1=$(date -d "" +%s) # date provided by input
dt1f=$(date -d @$dt1 +%F) # date1 formatted in YYYY-MM-DD
dt2=$((dt1 + 86400)) # add one day to it
dt2f=$(date -d @$dt2 +%F) # date2 formatted in YYYY-MM-DD
ifn=${2:-loghdg.txt} # input file
# printf "\nDate Range: %s - %s\n\n" "$(date -d @$dt1 +%F)" "$(date -d @$dt2 +%F)"
printf "\nDate Range: %s - %s\n\n" "$dt1f" "$dt2f"
# read each line in file
while read -r line; do
arr=( $line ) # read line into array
for i in ${arr[@]}; do # for each word in line
[ $i == $dt1f ] && found=1 # if date1 found, print lines
[ $i == $dt2f ] && found=0 # if date2 found, stop printing
done
[ "$found" == "1" ] && printf " %s\n" "$line"
done <"$ifn"
输入文件
$ cat loghdg.txt
Following are access details for date: 2015-02-18
details1
details2
....
....
Following are access details for date: 2015-02-19
line1
line2
....
....
Following are access details for date: 2015-02-20
line1
line2
....
....
output/use
$ sh date-day-diff.sh 2015-02-18
Date Range: 2015-02-18 - 2015-02-19
Following are access details for date: 2015-02-18
details1
details2
....
....
接受 start
和 end
日期的修改相当简单。
修改为接受开始+结束日期
dt1=$(date -d "" +%s) # start date provided by input
dt1f=$(date -d @$dt1 +%F) # date1 formatted in YYYY-MM-DD
dt2=$(date -d "" +%s) # end date provided by input
dt2f=$(date -d @$dt2 +%F) # date2 formatted in YYYY-MM-DD
ifn=${3:-loghdg.txt} # input file
新输出
$ sh date-day-diff.sh 2015-02-18 2015-02-20
Date Range: 2015-02-18 - 2015-02-20
Following are access details for date: 2015-02-18
details1
details2
....
....
Following are access details for date: 2015-02-19
line1
line2
....
....