以下命令:
grep -a volume somefile | awk '/^Apr 25 23:44:04*/,/^Apr 26 12:44:01*/ {print}'
工作正常,但是当我尝试通过将日期和时间放在变量中并执行时,它似乎不起作用。
time1="Apr 25 23:44:04"
time2="Apr 26 12:44:01"
grep -a volume somefile | awk '/^"$time1"/,/^"$time2"/ {print}'
最佳方法是使 awk
使用indata作为变量。
以Jonathans数据为例。
grep -a volume somedata | awk -v start="$time1" -v end="$time2" '$0~start {f=1} f; $0~end {f=0}'
Apr 25 23:44:04 Record 234404 volume
Apr 25 23:44:05 Record 234405 volume
Apr 25 23:44:06 Record 234406 volume
Apr 25 23:44:07 Record 234407 volume
Apr 25 23:44:08 Record 234408 volume
Apr 25 23:44:09 Record 234409 volume
Apr 25 23:44:10 Record 234410 volume
blank volume
Apr 26 00:00:00 Record 000000 volume
Apr 2009 Record 2009 volume
Apr 26 12:00:00 Record 120000 volume
blank volume
Apr 26 12:44:00 Record 124400 volume
Apr 26 12:44:01 Record 124401 volume
确保从行的开头击中数据。
grep -a volume somedata | awk -v start="$time1" -v end="$time2" 'BEGIN {start="^"start;end="^"end} $0~start {f=1} f; $0~end {f=0}'
在单引号中, $
没有意义。您目前有:
grep -a volume somefile | awk '/^"$time1"/,/^"$time2"/ {print}'
正在寻找包含双引号,美元符号,字母t,i,m,e,digit 1和另一个双引号的行 - 可能找不到。
您需要:
grep -a volume somefile | awk '/^'"$time1"'/,/^'"$time2"'/ {print}'
或(在这种情况下更简单,但通常不太可靠):
grep -a volume somefile | awk "/^$time1/,/^$time2/ {print}"
请注意,如果被搜索的模式包含斜线或后斜切或其他元载体,则需要逃脱它们。如果模式是简单的日期/时间字符串,则如此问题一样,这可以正常工作。
与"完美作品"版本相比,您还丢失了Regexes中的*
。这可能无关紧要。
示例输出
给定此脚本:
echo "Lines containing 'volume'"
echo
grep -a volume somefile
echo
echo "Version 1"
echo
grep -a volume somefile | awk '/^Apr 25 23:44:04*/,/^Apr 26 12:44:01*/ {print}'
echo
echo "Version 2"
echo
time1="Apr 25 23:44:04"
time2="Apr 26 12:44:01"
grep -a volume somefile | awk "/^$time1/,/^$time2/ {print}"
我得到输出:
Lines containing 'volume'
Apr 25 23:43:59 Record 234359 volume
Apr 25 23:44:00 Record 234400 volume
Apr 25 23:44:01 Record 234401 volume
Apr 25 23:44:02 Record 234402 volume
Apr 25 23:44:03 Record 234403 volume
Apr 25 23:44:04 Record 234404 volume
Apr 25 23:44:05 Record 234405 volume
Apr 25 23:44:06 Record 234406 volume
Apr 25 23:44:07 Record 234407 volume
Apr 25 23:44:08 Record 234408 volume
Apr 25 23:44:09 Record 234409 volume
Apr 25 23:44:10 Record 234410 volume
blank volume
Apr 26 00:00:00 Record 000000 volume
Apr 2009 Record 2009 volume
Apr 26 12:00:00 Record 120000 volume
blank volume
Apr 26 12:44:00 Record 124400 volume
Apr 26 12:44:01 Record 124401 volume
Apr 26 12:44:02 Record 124402 volume
Apr 26 12:44:03 Record 124403 volume
Apr 26 12:44:04 Record 124404 volume
Version 1
Apr 25 23:44:00 Record 234400 volume
Apr 25 23:44:01 Record 234401 volume
Apr 25 23:44:02 Record 234402 volume
Apr 25 23:44:03 Record 234403 volume
Apr 25 23:44:04 Record 234404 volume
Apr 25 23:44:05 Record 234405 volume
Apr 25 23:44:06 Record 234406 volume
Apr 25 23:44:07 Record 234407 volume
Apr 25 23:44:08 Record 234408 volume
Apr 25 23:44:09 Record 234409 volume
Apr 25 23:44:10 Record 234410 volume
blank volume
Apr 26 00:00:00 Record 000000 volume
Apr 2009 Record 2009 volume
Apr 26 12:00:00 Record 120000 volume
blank volume
Apr 26 12:44:00 Record 124400 volume
Version 2
Apr 25 23:44:04 Record 234404 volume
Apr 25 23:44:05 Record 234405 volume
Apr 25 23:44:06 Record 234406 volume
Apr 25 23:44:07 Record 234407 volume
Apr 25 23:44:08 Record 234408 volume
Apr 25 23:44:09 Record 234409 volume
Apr 25 23:44:10 Record 234410 volume
blank volume
Apr 26 00:00:00 Record 000000 volume
Apr 2009 Record 2009 volume
Apr 26 12:00:00 Record 120000 volume
blank volume
Apr 26 12:44:00 Record 124400 volume
Apr 26 12:44:01 Record 124401 volume
"包含'卷"的"线"实际上是所使用的数据文件。
"版本1"块表明REGEX Matter中的*
。
"版本2"块表明,通过仔细的引用,可以使日期可以正常工作,但是请注意,由于没有星星,输出与"版本1"不同。将星星添加回来将产生与"版本1"中相同的结果。