我想使用sed或gawk / awk命令来查找正则表达式:
TRAFFIC
并将其与以下unix命令或类似命令结合使用,该命令将sysdate减去一天:
date "--date=${dataset_date} -${date_diff} 1 day" +%d%b%Y
,然后删除regexp和sysdate之间的所有行,减去一天。
文件输入示例:
TYPE = REGIONAL (keep)
TRAFFIC AND LOGISTICS (keep)
(delete)
DATE (delete)
01AUG18 (delete)
TIME TRA NDFV (delete)
00:00-00:15 0.00 0 (delete)
00:15-00:30 0.00 0 (delete)
00:30-00:45 0.00 1 (delete)
DATE (delete)
02AUG18 (keep - sysdate minus one day)
TIME TRA NDFV (keep)
00:00-00:15 0.00 2 (keep)
00:15-00:30 0.00 0 (keep)
00:30-00:45 0.00 0 (keep)
00:45-01:00 0.00 0 (keep)
文件输出示例:
TYPE = REGIONAL
TRAFFIC AND LOGISTICS
02AUG18
TIME TRA NDFV
00:00-00:15 0.00 2
00:15-00:30 0.00 0
00:30-00:45 0.00 0
00:45-01:00 0.00 0
在这种情况下,02AUG18
将为sysdate减去一天。通常,正则表达式和sysdate之间会有更多的行减去要删除的行。是否可以结合以下命令:
sed '/TRAFFIC/,/ [sysdate minus one day] /d'
答案 0 :(得分:1)
尝试awk:
d=$( date -d yesterday "+%d%b%y" | tr '[:lower:]' '[:upper:]' )
awk -v date="$d" '$1 == date {del = 0}; !del; /TRAFFIC/ {del = 1}'
答案 1 :(得分:0)
打印前两行,然后打印从sysdate -1到另一个DATE的范围:
sed -n -e '1,2p' -e "/$(date -v-1d +%d%b%y | tr -s '[:lower:]' '[:upper:]')/,/DATE/p"
输出:
TYPE = REGIONAL (keep)
TRAFFIC AND LOGISTICS (keep)
20AUG18 (keep - sysdate minus one day)
TIME TRA NDFV (keep)
00:00-00:15 0.00 2 (keep)
00:15-00:30 0.00 0 (keep)
00:30-00:45 0.00 0 (keep)
00:45-01:00 0.00 0 (keep)
要点是,是的,您可以将表达式传递给sed;在这种情况下,作为范围的一部分。注意:(1)我假设第二个日期之后可能还有另一个DATE,因此用DATE关闭了该范围,但是您可能还需要其他内容。