我有一个文件,其中包含一些输出,例如:
1395943855344,187,HTTP Request,200,OK,TID Requests 1-1,text,true,6730,184
1395943855546,17,HTTP Request,200,OK,TID Requests 1-1,text,true,517,17
1395943855565,131,HTTP Request,200,OK,TID Requests 1-1,text,true,7453,128
1395943855703,16,HTTP Request,200,OK,TID Requests 1-1,text,true,517,16
1395943855723,140,HTTP Request,200,OK,TID Requests 1-1,text,true,7629,137
1395943855782,204,HTTP Request,200,OK,TID Requests 1-2,text,true,8160,200
1395943855870,15,HTTP Request,200,OK,TID Requests 1-1,text,true,518,15
1395943855890,147,HTTP Request,200,OK,TID Requests 1-1,text,true,8178,143
1395943856003,17,HTTP Request,200,OK,TID Requests 1-2,text,true,516,17
1395943856025,152,HTTP Request,200,OK,TID Requests 1-2,text,true,8113,148
1395943856044,18,HTTP Request,200,OK,TID Requests 1-1,text,true,518,18
1395943856068,126,HTTP Request,200,OK,TID Requests 1-1,text,true,7961,122
1395943856175,179,HTTP Request,200,OK,TID Requests 1-3,text,true,7901,175
1395943879200,79,HTTP Request,200,OK,TID requests 2-57,text,true,226,79
1395943879201,100,HTTP Request,200,OK,TID requests 2-89,text,true,226,100
1395943879201,27,HTTP Request,200,OK,TID requests 5-135,text,true,226,27
1395943879201,289,HTTP Request,200,OK,TID requests 4-9,text,true,226,289
1395943879201,294,HTTP Request,200,OK,TID requests 4-138,text,true,226,294
1395943879201,367,HTTP Request,200,OK,TID requests 2-17,text,true,226,367
1395943879201,369,HTTP Request200,OK,TID Requests 1-107,text,true,7781,301
1395943879201,86,HTTP Request,200,OK,TID requests 3-91,text,true,226,86
1395943879201,86,HTTP Request,200,OK,TID requests 4-26,text,true,226,86
1395943879202,122,HTTP Request,200,OK,TID requests 2-99,text,true,226,122
1395943879202,125,HTTP Request,200,OK,TID requests 4-10,text,true,226,125
1395943979202,312,HTTP Request,200,OK,TID requests 3-86,text,true,226,312
1395943979202,376,HTTP Request,200,OK,TID requests 2-49,text,true,226,376
1395943979202,86,HTTP Request,200,OK,TID requests 2-126,text,true,226,86
1395943979202,89,HTTP Request,200,OK,TID requests 3-75,text,true,226,89
1395943979202,94,HTTP Request,200,OK,TID requests 2-149,text,true,226,94
1395943979203,129,HTTP Request,200,OK,TID requests 2-45,text,true,226,129
1395943979203,134,HTTP Request,200,OK,TID requests 2-26,text,true,226,134
1395943979203,37,HTTP Request,200,OK,TID requests 2-51,text,true,226,37
1395943979203,89,HTTP Request,200,OK,TID requests 2-55,text,true,226,89
1395943979204,123,HTTP Request,200,OK,TID requests 2-85,text,true,226,123
1395943979204,93,HTTP Request,200,OK,TID requests 2-53,text,true,226,93
我正在尝试获取从1395943855到1395943879的一系列线路。我可以在命令行中通过发出:来做到这一点
awk '/1395943855[0-9]+/,/1395943879[0-9]+/' input_file.txt
我想用shell或bash脚本编程实现这一点。其中1395943879和139594387是输入到脚本中的变量。
我试过了,但没有成功,也不知道为什么。
echo 1395943855 1395943879 | awk -v v1=$1 -v v2=$2 '/v1[0-9]+/,/v2[0-9]+/' input_file.txt
v1
在awk /regex/
命令中并不特殊。此外,$1
指的是脚本参数,而不是写入stdin的任何内容。
与其尝试组合和匹配regex,不如根据数字是否在某个范围内打印行?
start=1395943855000
end=1395943879999
awk -F, -v "from=$start" -v "to=$end" '$1 >= from && $1 <= to' input_file.txt
如果您不想包括以1395943879:开头的行
awk -v from=1395943855 -v to=1395943879 '
$1 ~ "^"from {p=1}
$1 ~ "^"to {exit}
p
' file
如果你真的想包括它们:
awk -v from=1395943855 -v to=1395943879 '
$1 ~ "^"from {p=1}
$1 ~ "^"to {end=$1}
end && $1 != end {exit}
p
' file
将其放入脚本中:
#!/bin/sh
awk -v from="$1" -v to="$2" '
$1 ~ "^"from {p=1}
$1 ~ "^"to {end=$1}
end && $1 != end {exit}
p
' file
并调用它,如:./script.sh 1395943855 1395943879
如果输入文件按第一列排序,我们可以避免以下比较:
start=1395943855000
end=1395943879999
awk -F, -v start=$start -v end=$end '$1 < start { next } $1 > end { exit } { print }' input_file.txt