免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4454 | 回复: 13

[文本处理] 【已解决】awk 取特定的行 [复制链接]

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-07-23 06:20:00数据库技术版块每日发帖之星
日期:2016-07-29 06:20:00
发表于 2016-07-12 12:02 |显示全部楼层
本帖最后由 zhaiyuzhen 于 2017-04-26 16:57 编辑

locked.txt文件见最后。我想取trx_state:为LOCK WAIT的第一行、第六行。trx_state为RUNNING的部分不取。我度娘、谷歌了两天了,感觉自己缺乏逻辑性,所以特地求教大家,谢谢!
预期的结果为:
trx_id: 3377
trx_query: update test10.testid set name=' qqq' where id = 7
trx_id: 3378
trx_query: update test10.testid set name=' aaa' where id = 6
trx_id: 3387
trx_query: update test10.testid set name=' qq' where id = 9
trx_id: 3477
trx_query: update test10.testid set name=' qqqq' where id = 60

locked.txt内容:
*************************** 1. row ***************************
                    trx_id: 3377
                 trx_state: LOCK WAIT
               trx_started: 2016-07-11 15:56:46
     trx_requested_lock_id: 3377:6:3:5
          trx_wait_started: 2016-07-11 16:10:58
                trx_weight: 2
       trx_mysql_thread_id: 3
                 trx_query: update test10.testid set name=' qqq' where id = 7
       trx_operation_state: starting index read
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 2
     trx_lock_memory_bytes: 1184
           trx_rows_locked: 1
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 2. row ***************************
                    trx_id: 3372
                 trx_state: RUNNING
               trx_started: 2016-07-11 15:54:24
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 5
       trx_mysql_thread_id: 4
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         trx_tables_locked: 0
          trx_lock_structs: 3
     trx_lock_memory_bytes: 360
           trx_rows_locked: 2
         trx_rows_modified: 2
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 3. row ***************************
                    trx_id: 3378
                 trx_state: LOCK WAIT
               trx_started: 2016-07-11 15:56:46
     trx_requested_lock_id: 3377:6:3:5
          trx_wait_started: 2016-07-11 16:10:58
                trx_weight: 2
       trx_mysql_thread_id: 3
                 trx_query: update test10.testid set name=' aaa' where id = 6
       trx_operation_state: starting index read
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 2
     trx_lock_memory_bytes: 1184
           trx_rows_locked: 1
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 4. row ***************************
                    trx_id: 3387
                 trx_state: LOCK WAIT
               trx_started: 2016-07-11 15:56:46
     trx_requested_lock_id: 3377:6:3:5
          trx_wait_started: 2016-07-11 16:10:58
                trx_weight: 2
       trx_mysql_thread_id: 3
                 trx_query: update test10.testid set name=' qq' where id = 9
       trx_operation_state: starting index read
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 2
     trx_lock_memory_bytes: 1184
           trx_rows_locked: 1
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 5. row ***************************
                    trx_id: 3477
                 trx_state: LOCK WAIT
               trx_started: 2016-07-11 15:56:46
     trx_requested_lock_id: 3377:6:3:5
          trx_wait_started: 2016-07-11 16:10:58
                trx_weight: 2
       trx_mysql_thread_id: 3
                 trx_query: update test10.testid set name=' qqqq' where id = 60
       trx_operation_state: starting index read
         trx_tables_in_use: 1
         trx_tables_locked: 1
          trx_lock_structs: 2
     trx_lock_memory_bytes: 1184
           trx_rows_locked: 1
         trx_rows_modified: 0
   trx_concurrency_tickets: 0
       trx_isolation_level: REPEATABLE READ
         trx_unique_checks: 1
    trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 10000
          trx_is_read_only: 0
trx_autocommit_non_locking: 0

论坛徽章:
6
15-16赛季CBA联赛之新疆
日期:2016-03-22 22:34:5915-16赛季CBA联赛之山东
日期:2016-04-11 09:08:41程序设计版块每日发帖之星
日期:2016-06-28 06:20:00程序设计版块每日发帖之星
日期:2016-07-19 06:20:00每日论坛发贴之星
日期:2016-07-19 06:20:0015-16赛季CBA联赛之青岛
日期:2016-07-20 22:44:17
发表于 2016-07-12 12:42 来自手机 |显示全部楼层
本帖最后由 RE_HASH 于 2016-07-12 12:42 编辑


#! /bin/sh
# my.profile
host1=www.sina.com.cn
host3=www.baidu.com
host4=www.taobao.com

论坛徽章:
54
2015亚冠之德黑兰石油
日期:2015-07-07 13:00:1615-16赛季CBA联赛之深圳
日期:2016-03-31 09:03:5415-16赛季CBA联赛之辽宁
日期:2016-05-09 20:38:15程序设计版块每日发帖之星
日期:2016-05-12 06:20:0015-16赛季CBA联赛之四川
日期:2016-05-13 15:19:4715-16赛季CBA联赛之福建
日期:2016-05-15 20:24:34每日论坛发贴之星
日期:2016-05-16 06:20:0015-16赛季CBA联赛之吉林
日期:2016-05-26 11:49:4715-16赛季CBA联赛之广东
日期:2016-05-26 13:49:18极客徽章
日期:2016-12-07 14:05:2315-16赛季CBA联赛之广夏
日期:2016-12-20 17:33:532017金鸡报晓
日期:2017-01-10 15:19:56
发表于 2016-07-12 12:47 |显示全部楼层
  1. awk -vRS='\\*+' -F'\n' '/LOCK WAIT/&&$0=$2"\n"$9'
复制代码

论坛徽章:
2
IT运维版块每日发帖之星
日期:2016-07-23 06:20:00数据库技术版块每日发帖之星
日期:2016-07-29 06:20:00
发表于 2016-07-12 12:47 |显示全部楼层
本帖最后由 zhaiyuzhen 于 2016-07-12 12:53 编辑

感谢楼上帮忙,确实可行。谢谢!
顺便解释一下,给后来的同学参考:
答主的思路是以“LOCK WAIT”为关键字,列出该关键字的前一行至后六行的内容,然后再用|grep -P 'trx_id|trx_query' 过滤一下,思路很巧妙!我不知道grep 还有A、B选项,基本功不扎实啊!

论坛徽章:
28
15-16赛季CBA联赛之八一
日期:2016-02-22 19:10:4215-16赛季CBA联赛之深圳
日期:2016-12-01 10:34:0415-16赛季CBA联赛之新疆
日期:2016-12-07 10:24:2915-16赛季CBA联赛之同曦
日期:2016-12-15 12:06:43CU十四周年纪念徽章
日期:2016-12-18 13:03:4415-16赛季CBA联赛之吉林
日期:2017-01-03 15:52:2515-16赛季CBA联赛之辽宁
日期:2017-01-04 14:58:2415-16赛季CBA联赛之辽宁
日期:2017-01-15 09:42:512016科比退役纪念章
日期:2017-02-06 17:21:50黑曼巴
日期:2017-02-10 15:46:1215-16赛季CBA联赛之上海
日期:2017-03-18 10:14:5415-16赛季CBA联赛之青岛
日期:2017-03-18 22:00:44
发表于 2016-07-12 13:22 |显示全部楼层
本帖最后由 moperyblue 于 2016-07-12 18:17 编辑
  1. sed '/trx_id/{:a;N;/trx_query/!ba;/LOCK WAIT/{s/\n.*\n/\n/p}};d'
复制代码
  1. sed -n '/trx_id/{N;/LOCK WAIT/{P;:a;n;/trx_query/!ba;p}};d'
复制代码
  1. sed '/trx_id/{N;/LOCK WAIT/{:a;N;/trx_query/!ba;s/\n.*\n/\n/p}};d'
复制代码

论坛徽章:
2
luobin
日期:2016-06-17 17:46:36lufei
日期:2016-06-17 17:49:16
发表于 2016-07-12 13:28 |显示全部楼层
grep " trx_state: LOCK WAIT" -A6 -B1 locked.txt | awk 'NR%9==1||NR%9==8{print $0}'
这样么?

论坛徽章:
10
15-16赛季CBA联赛之同曦
日期:2016-06-11 19:22:4115-16赛季CBA联赛之深圳
日期:2020-05-31 16:13:5615-16赛季CBA联赛之同曦
日期:2020-01-28 12:42:47每日论坛发贴之星
日期:2016-08-09 06:20:00程序设计版块每日发帖之星
日期:2016-08-09 06:20:00每日论坛发贴之星
日期:2016-07-12 06:20:00程序设计版块每日发帖之星
日期:2016-07-12 06:20:00程序设计版块每日发帖之星
日期:2016-07-06 06:20:00程序设计版块每日发帖之星
日期:2016-07-04 06:20:0015-16赛季CBA联赛之佛山
日期:2021-02-26 09:33:41
发表于 2016-07-12 13:30 |显示全部楼层
awk -F':' '/trx_id/{a=$0;d=0}/trx_query/&&d!=1{b=$0;print a"\n"c"\n"b}/trx_state/{if($2~/RUNNING/){d=1}c=$0}'

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
发表于 2016-07-12 13:45 |显示全部楼层
本帖最后由 sunzhiguolu 于 2016-07-12 13:47 编辑
  1. perl -ne 'BEGIN{$r=qr/LOCK WAIT/}{if(/\A\*/){print @a[0,7] if($a[1]=~$r);@a=();next}push(@a,$_)}END{print @a[0,7] if($a[1]=~$r)}' f
复制代码
trx_id: 3377
                 trx_query: update test10.testid set name=' qqq' where id = 7
                    trx_id: 3378
                 trx_query: update test10.testid set name=' aaa' where id = 6
                    trx_id: 3387
                 trx_query: update test10.testid set name=' qq' where id = 9
                    trx_id: 3477
                 trx_query: update test10.testid set name=' qqqq' where id = 60

论坛徽章:
6
羊年新春福章
日期:2015-03-03 17:16:28双子座
日期:2015-03-03 17:16:56巳蛇
日期:2015-03-03 17:17:2415-16赛季CBA联赛之福建
日期:2016-03-11 09:05:00黑曼巴
日期:2016-07-07 16:58:1215-16赛季CBA联赛之吉林
日期:2016-11-14 09:23:07
发表于 2016-07-12 14:05 |显示全部楼层
  1. awk '!/LOCK WAIT/{m=$0}/LOCK WAIT/{x=NR+6;print m}NR==x{print}' file
复制代码

求职 : 技术支持/维
论坛徽章:
0
发表于 2016-07-12 14:08 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP