测试文件
cat text.txt
09:00:00 TESTING
09:00:01 TESTING
09:00:02 TESTING
09:00:03 TESTING
09:00:04 TESTING
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
09:02:01 TESTING
09:02:02 TESTING
09:02:03 TESTING
09:03:00 TESTING
09:03:01 TESTING
09:03:02 TESTING
09:03:03 TESTING
没有变量的实际模式搜索可以正常工作:
cat ./text.txt|awk -v start="09:01" -v end="09:02" '/09:01/,/09:02/' text.txt
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
使用失败的变量进行模式搜索
awk -v start="09:01" -v end="09:02" '/start/,/end/' text.txt
讨厌的 hack 使用 shell echo 包装变量,使其工作
echo "awk -v start="09:01" -v end="09:02" '/$start/,/$end/' text.txt"
awk -v start=09:01 -v end=09:02 '/09:01/,/09:02/' text.txt
start="09:01"; end="09:02"; echo "awk -v start="09:01" -v end="09:02 text.txt" '/$start/,/$end/'"|/bin/sh
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
关于我做错了什么或者这是否是 awk 中可能的功能的任何想法?
这些建议奏效了,我的结果有所不同:
1.
awk -v start="09:01" -v end="09:02" '$0~start,$0~end' text.txt
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
2.
awk -v start="09:01" -v end="09:02" 'match($0,start),match($0,end)' text.txt
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
3.
awk -v start="09:01" -v end="09:02" '$0~start{print}$0~end{print}' text.txt
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
09:02:01 TESTING
09:02:02 TESTING
09:02:03 TESTING
4.
awk -v start="09:01" -v end="09:02" '$1~start||$1~end' text.txt
09:01:00 TESTING
09:01:01 TESTING
09:01:02 TESTING
09:01:03 TESTING
09:02:00 TESTING
09:02:01 TESTING
09:02:02 TESTING
09:02:03 TESTING
3 和 4 返回完整的结果 前两个在到达模式后立即停止