我有一个文件包含如下输出:

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和1395943879是输入到脚本中的变量。
我试过了,但没用,也不知道为什么。
echo 1395943855 1395943879 | awk -v v1=$1 -v v2=$2 '/v1[0-9]+/,/v2[0-9]+/' 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

08-16 10:14