将日期和时间模式传递给awk

时间:2015-05-01 05:26:22

标签: bash awk

以下命令:

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}'

2 个答案:

答案 0 :(得分:2)

最好的方法是让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}'

答案 1 :(得分:1)

在单引号内,$没有任何意义。你现在有:

grep -a volume somefile | awk '/^"$time1"/,/^"$time2"/ {print}'

正在寻找包含双引号,美元符号,字母t,i,m,e,数字1和另一个双引号的行 - 它可能无法找到它。< / p>

你需要:

grep -a volume somefile | awk '/^'"$time1"'/,/^'"$time2"'/ {print}'

或(在这种情况下更简单,但一般不太可靠):

grep -a volume somefile | awk "/^$time1/,/^$time2/ {print}"

请注意,如果要搜索的模式包含斜杠或反斜杠或其他元字符,则需要将它们转义。如果模式是简单的日期/时间字符串,就像在这个问题中一样,这很好。

与“完美”工作相比,您在正则表达式中也丢失了*&#39;版。这可能也可能不重要。

示例输出

鉴于此脚本:

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

&#34;包含&#39;&#39;&#34;&#34;实际上是正在使用的数据文件。

&#34;版本1&#34;块显示正则表达式中的*很重要。

&#34;版本2&#34;块显示,仔细引用,日期可以工作,但请注意输出不同于&#34;版本1&#34;因为没有星星。添加星星将产生与&#34;版本1&#34;。

相同的结果