首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从shell输入到文件中的awk范围

从shell输入到文件中的awk范围
EN

Stack Overflow用户
提问于 2014-04-15 21:32:02
回答 3查看 85关注 0票数 0

我有一个文件,其中包含这样的一些输出:

代码语言:javascript
复制
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之间找出一系列的线路。我可以在命令行中通过发出:

代码语言:javascript
复制
awk '/1395943855[0-9]+/,/1395943879[0-9]+/' input_file.txt

我想从shell或bash脚本中编程地这样做。其中1395943879和1395943879是输入到脚本中的变量。

我试过了,但没有成功,也不知道为什么。

代码语言:javascript
复制
echo 1395943855 1395943879 | awk -v v1=$1 -v v2=$2 '/v1[0-9]+/,/v2[0-9]+/' input_file.txt
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-15 22:53:56

如果不想包括以1395943879开头的行:

代码语言:javascript
复制
awk -v from=1395943855 -v to=1395943879 '
    $1 ~ "^"from {p=1} 
    $1 ~ "^"to   {exit} 
    p
' file

如果你真的想把它们包括在内:

代码语言:javascript
复制
awk -v from=1395943855 -v to=1395943879 '
    $1 ~ "^"from {p=1} 
    $1 ~ "^"to   {end=$1} 
    end && $1 != end {exit} 
    p
' file

把它写进剧本里:

代码语言:javascript
复制
#!/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

票数 0
EN

Stack Overflow用户

发布于 2014-04-15 21:46:49

v1在awk /regex/命令中并不特殊。另外,$1引用的是脚本参数,而不是任何写入stdin的内容。

与其尝试撰写和匹配regex,不如仅仅根据数字是否在某个范围内打印行如何?

代码语言:javascript
复制
start=1395943855000
end=1395943879999
awk -F, -v "from=$start" -v "to=$end" '$1 >= from && $1 <= to' input_file.txt
票数 4
EN

Stack Overflow用户

发布于 2014-04-15 22:01:17

如果输入文件在第一列上排序,我们可以避免进行如下比较:

代码语言:javascript
复制
start=1395943855000
end=1395943879999
awk -F, -v start=$start -v end=$end '$1 < start { next } $1 > end { exit } { print }' input_file.txt
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23094936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档