Jas*_*n G 9 bash text-processing
我被困在如何删除比给定日期新的行上。这是文件内容的片段。
构建保存.txt
647919 2013/11/30
647946 2013/11/30
647955 2013/12/01
648266 2013/12/03
648267 2013/12/03
648674 2013/12/04
Run Code Online (Sandbox Code Playgroud)
我想删除比 2013/12/03 新的行,只留下
647919 2013/11/30
647946 2013/11/30
647955 2013/12/01
Run Code Online (Sandbox Code Playgroud)
这如何通过 bash 完成?
如果您的系统包含该date命令的 GNU 版本,您可以使用它来将日期字段(在去除尾随后<br>,如果存在)转换为自纪元以来的秒数,并直接与相同格式的截止日期进行比较,例如在 bash 中
testsecs=$(date +%s --date="2013/12/03")
while IFS= read -r line; do
read -r x d <<< "$line"
if (( $(date +%s --date="${d%<br>}") < $testsecs )); then
printf '%s\n' "$line"
fi
done < buildsave.txt
Run Code Online (Sandbox Code Playgroud)
[请注意,这不会执行就地删除 - 您需要将结果保存到临时文件并重命名。]