免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: nonamexz
打印 上一主题 下一主题

一个sed匹配的问题求助. [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-05-11 08:47 |只看该作者
  1. awk '/Query_time/{if($3>30){matched=$0;getline;print matched"\n"$0}}' file
复制代码

还有烈火个火王没过来呢

论坛徽章:
0
12 [报告]
发表于 2009-05-11 09:45 |只看该作者
原帖由 greendays 于 2009-5-11 08:43 发表
grep -A1 'Query_time' urfile | sed -n 'N;s/\n/###/p' | awk '/Query_time/{if($3>30) print}' |sed 's/###/\n/'


这方法有点问题,这是测试结果的一部分:

# Query_time: 298.875456  Lock_time: 0.000088 Rows_sent: 364  Rows_examined: 14727555
--
# Query_time: 254.052164  Lock_time: 0.000118 Rows_sent: 1  Rows_examined: 14752217
--
# Query_time: 402.263098  Lock_time: 0.000107 Rows_sent: 2  Rows_examined: 58095
--
# Query_time: 117.647185  Lock_time: 0.000121 Rows_sent: 1  Rows_examined: 4845959
# Query_time: 117.115601  Lock_time: 0.000116 Rows_sent: 1  Rows_examined: 4845959
select count(*),sum(amount),sum(creditAmount),sum(useAmount),sum(usePresent),sum(point),sum(timeConsume)/3600 from Consume  where netBarId=777 and submitTime>='2009-04-01 00:00:00' and submitTime<'2009-05-01 00:00:00';
--
# Query_time: 136.982316  Lock_time: 0.000188 Rows_sent: 1  Rows_examined: 4845957
--
# Query_time: 110.424634  Lock_time: 0.000151 Rows_sent: 1000  Rows_examined: 4848716

论坛徽章:
0
13 [报告]
发表于 2009-05-11 09:53 |只看该作者
如果文件的内容是这样的:
# Time: 090511  3:33:17
# User@Host: viewer[viewer] @ localhost [127.0.0.1]
# Query_time: 18.463793  Lock_time: 0.000088 Rows_sent: 20  Rows_examined: 4855
SET timestamp=1241983997;
select * from Member where Id=898 and classId=3;
# Time: 090511  7:32:49
# User@Host: viewer[viewer] @ localhost [127.0.0.1]
# Query_time: 40.986851  Lock_time: 0.000110 Rows_sent: 15  Rows_examined: 9342
SET timestamp=1241998369;
select * from Member where Id=879 and classId=3 and available=0;
# Time: 090511  8:10:29
# User@Host: viewer[viewer] @ localhost [127.0.0.1]
# Query_time: 16.822295  Lock_time: 0.000101 Rows_sent: 25  Rows_examined: 6185
SET timestamp=1242000629;
select * from Member where Id=894 and classId=14 and available=2;
# Time: 090511  9:07:45
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 311.937897  Lock_time: 0.000113 Rows_sent: 2  Rows_examined: 59032
SET timestamp=1242004065;
select * from Consume where Id=1096 and pcClass=1 and amount+creditAmount+useAmount=0.0 and termId='A022' and submitTime>='2009-05-08 00:00:00' and submitTime<'2009-05-09 00:00:00' order by submitTime desc;
# Time: 090511  9:27:17
# User@Host: root[root] @ localhost [127.0.0.1]
# Query_time: 12.207931  Lock_time: 0.000191 Rows_sent: 2  Rows_examined: 2764
SET timestamp=1242005237;
select * from Consume where Id=1096 and pcClass=1 and amount+creditAmount+useAmount=0.0 and termId='A022' and submitTime>='2009-05-08 00:00:00' and submitTime<'2009-05-09 00:00:00' order by submitTime desc;

要求查找匹配Query_time值大于30的行,并将它上面的第二行,本行和下面一行打印出来,如:

# Time: 090511  7:32:49
# Query_time: 40.986851  Lock_time: 0.000110 Rows_sent: 15  Rows_examined: 9342
select * from Member where Id=879 and classId=3 and available=0;

论坛徽章:
0
14 [报告]
发表于 2009-05-11 11:03 |只看该作者
不错!sed 实现起来困难啊!!

论坛徽章:
0
15 [报告]
发表于 2009-05-11 11:26 |只看该作者
tmp]# grep -A 1 "`awk '/Query_time/{if($3>30) print $0}' t.txt`" t.txt  
# Query_time: 30.170243  Lock_time: 0.000109 Rows_sent: 1  Rows_examined: 8131
select count(*),sum(returnAmount),sum(roundAmount),sum(deposit),sum(costExpense) from Returned;

论坛徽章:
0
16 [报告]
发表于 2009-05-11 13:02 |只看该作者
原帖由 nonamexz 于 2009-5-11 09:53 发表
如果文件的内容是这样的:
# Time: 090511  3:33:17
# User@Host: viewer[viewer] @ localhost [127.0.0.1]
# Query_time: 18.463793  Lock_time: 0.000088 Rows_sent: 20  Rows_examined: 4855
SET timest ...


#!/usr/local/bin/bash
for i in $(awk '$2~/Query_time/ && $3 > 30 {print NR}' ufile)
do
sed -n "$((${i}-2))p;${i}p;$((${i}+2))p" ufile
done

./test
# Time: 090511  7:32:49
# Query_time: 40.986851  Lock_time: 0.000110 Rows_sent: 15  Rows_examined: 9342
select * from Member where Id=879 and classId=3 and available=0;
# Time: 090511  9:07:45
# Query_time: 311.937897  Lock_time: 0.000113 Rows_sent: 2  Rows_examined: 59032
select * from Consume where Id=1096 and pcClass=1 and amount+creditAmount+useAmount=0.0 and termId='A022' and submitTime>='2009-05-08 00:00:00' and submitTime<'2009-05-09 00:00:00' order by submitTime desc;

[ 本帖最后由 steel_lei 于 2009-5-11 13:09 编辑 ]

论坛徽章:
0
17 [报告]
发表于 2009-05-11 13:20 |只看该作者
awk '/Query_time/&&$3>30{print;T=1;next}T{print;T=0}' file

论坛徽章:
0
18 [报告]
发表于 2009-05-11 15:03 |只看该作者
原帖由 ywlscpl 于 2009-5-11 13:20 发表
awk '/Query_time/&&$3>30{print;T=1;next}T{print;T=0}' file

能解釋一下T在這里有什么作用嗎?

论坛徽章:
0
19 [报告]
发表于 2009-05-11 17:19 |只看该作者
原帖由 star_wm_craft 于 2009-5-11 15:03 发表

能解釋一下T在這里有什么作用嗎?

一个自己设置的flag,在/Query_time/&&$3>30条件匹配到的行设置为非零,然后next调用下一行,由于此时flag非零,所以会执行{print;T=0},print之后flag重置为零。

论坛徽章:
0
20 [报告]
发表于 2009-05-12 11:20 |只看该作者
原帖由 kwokcn 于 2009-5-11 17:19 发表

一个自己设置的flag,在/Query_time/&&$3>30条件匹配到的行设置为非零,然后next调用下一行,由于此时flag非零,所以会执行{print;T=0},print之后flag重置为零。

如果要匹配到的行的上一行和下一行怎么寫?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP