匹配两行,然后获取直到空行的一个段落

在处理数据库输出时,需要匹配特定行并获取直到空行的段落,例如从'getsnapshot for tables on dbname'中提取'db2inst1.student'表的信息。目标是输出'Schema'和'Table Name'所在的段落。通过研究类似案例和学习sed命令,如'h;H和:a;N;ba'的用法,可以实现对这类段落的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要获取getsnapshot for tables on dbname输出的表信息中一个表的信息,我们需要匹配到两行,然后输出一段这种就不是之前简单的列输出了,这个要怎么办呢?

[db2inst1@db22 ~]$ db2 get snapshot for tables on kilo

            Table Snapshot

First database connect timestamp     = 09/22/2017 10:41:19.323227
Last reset timestamp                 =
Snapshot timestamp                   = 09/25/2017 09:21:52.828200
Database name                        = KILO
Database path                        = /home/db2inst1/db2inst1/NODE0000/SQL00002/MEMBER0000/
Input database alias                 = KILO
Number of accessed tables            = 12

Table List
 Table Schema        = SYSIBM
 Table Name          = SYSTABLES
 Table Type          = Catalog
 Data Object Pages   = 90
 Index Object Pages  = 50
 LOB Object pages    = 1088
 Rows Read           = Not Collected
 Rows Written        = 55
 Overflows           = 36
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSCOLUMNS
 Table Type          = Catalog
 Data Object Pages   = 423
 Index Object Pages  = 193
 LOB Object pages    = 1
 Rows Read           = Not Collected
 Rows Written        = 18
 Overflows           = 0
 Page Reorgs         = 4

 Table Schema        = SYSIBM
 Table Name          = SYSINDEXES
 Table Type          = Catalog
 Data Object Pages   = 43
 Index Object Pages  = 25
 LOB Object pages    = 1
 Rows Read           = Not Collected
 Rows Written        = 60
 Overflows           = 35
 Page Reorgs         = 1

 Table Schema        = SYSTOOLS
 Table Name          = HMON_ATM_INFO
 Table Type          = User
 Data Object Pages   = 15
 Index Object Pages  = 6
 Rows Read           = Not Collected
 Rows Written        = 1799
 Overflows           = 6243
 Page Reorgs         = 1

 Table Schema        = SYSIBM
 Table Name          = SYSINDEXCOLUSE
 Table Type          = Catalog
 Data Object Pages   = 18
 Index Object Pages  = 23
 LOB Object pages    = 1
 Rows Read           = Not Collected
 Rows Written        = 1
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSDATAPARTITIONS
 Table Type          = Catalog
 Data Object Pages   = 8
 Index Object Pages  = 23
 Rows Read           = Not Collected
 Rows Written        = 5
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSPLAN
 Table Type          = Catalog
 Data Object Pages   = 47
 Index Object Pages  = 23
 LOB Object pages    = 448
 Rows Read           = Not Collected
 Rows Written        = 4
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSTABAUTH
 Table Type          = Catalog
 Data Object Pages   = 11
 Index Object Pages  = 30
 Rows Read           = Not Collected
 Rows Written        = 1
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSINDEXAUTH
 Table Type          = Catalog
 Data Object Pages   = 1
 Index Object Pages  = 10
 Rows Read           = Not Collected
 Rows Written        = 1
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = SYSIBM
 Table Name          = SYSCOLDIST
 Table Type          = Catalog
 Data Object Pages   = 581
 Index Object Pages  = 627
 Rows Read           = Not Collected
 Rows Written        = 1980
 Overflows           = 0
 Page Reorgs         = 17

 Table Schema        = SYSIBM
 Table Name          = SYSUSERAUTH
 Table Type          = Catalog
 Data Object Pages   = 11
 Index Object Pages  = 16
 LOB Object pages    = 1
 Rows Read           = Not Collected
 Rows Written        = 1
 Overflows           = 0
 Page Reorgs         = 0

 Table Schema        = DB2INST1
 Table Name          = STUDENT
 Table Type          = User
 Data Object Pages   = 60
 Index Object Pages  = 34
 Rows Read           = Not Collected
 Rows Written        = 5283
 Overflows           = 0
 Page Reorgs         = 119
 Table Reorg Information:
   Reorg Type        =
        Reclustering
        Inplace Table Reorg
        Allow Write Access
   Reorg Index       = 1
   Reorg Tablespace  = 2
   Start Time        = 09/22/2017 14:19:56.266280
   Reorg Phase       =
   Max Phase         =
   Phase Start Time  =
   Status            = Completed
   Current Counter   = 0
   Max Counter       = 0
   Completion        = 0
   End Time          = 09/22/2017 14:19:57.358522

目标:匹配表 db2inst1.student,输出
Table Schema = DB2INST1
Table Name = STUDENT
这一段内容。

首先百度一下,然后看到一些有相似之处的案例先研究一下:

[db2inst1@db22 ~]$ db2 get snapshot for tables on kilo | sed -n '/^ Table Schema[ ]* = DB2INST1/N;x;/STUDENT/p'
 Table Schema        = DB2INST1
 Table Name          = STUDENT
[db2inst1@db22 ~]$ db2 get snapshot for tables on kilo | sed -n '/^ Table Schema[ ]* = DB2INST1/N;x;/STUDENT/{:a;N;/\n$/!{$!ba};p}'
 Table Schema        = DB2INST1
 Table Name          = STUDENT
 Data Object Pages   = 60
 Index Object Pages  = 34
 Rows Read           = Not Collected
 Rows Written        = 5283
 Overflows           = 0
 Page Reorgs         = 119
 Table Reorg Information:
   Reorg Type        =
        Reclustering
        Inplace Table Reorg
        Allow Write Access
   Reorg Index       = 1
   Reorg Tablespace  = 2
   Start Time        = 09/22/2017 14:19:56.266280
   Reorg Phase       =
   Max Phase         =
   Phase Start Time  =
   Status            = Completed
   Current Counter   = 0
   Max Counter       = 0
   Completion        = 0
   End Time          = 09/22/2017 14:19:57.358522
[db2inst1@db22 ~]$ she='DB2INST1'
[db2inst1@db22 ~]$ tb='STUDENT'
[db2inst1@db22 ~]$ db2 get snapshot for tables on kilo | sed -n '/^ Table Schema[ ]* = '"${she}"'/N;x;/Table Name[ ]*= '"${tb}"'/{:a;N;/\n$/!{$!ba};p}'
 Table Schema        = DB2INST1
 Table Name          = STUDENT
 Data Object Pages   = 60
 Index Object Pages  = 34
 Rows Read           = Not Collected
 Rows Written        = 5283
 Overflows           = 0
 Page Reorgs         = 119
 Table Reorg Information:
   Reorg Type        =
        Reclustering
        Inplace Table Reorg
        Allow Write Access
   Reorg Index       = 1
   Reorg Tablespace  = 2
   Start Time        = 09/22/2017 14:19:56.266280
   Reorg Phase       =
   Max Phase         =
   Phase Start Time  =
   Status            = Completed
   Current Counter   = 0
   Max Counter       = 0
   Completion        = 0
   End Time          = 09/22/2017 14:19:57.358522

[db2inst1@db22 ~]$ db2 get snapshot for tables on kilo | sed -n '/^ Table Schema[ ]*= DB2INST1/N;x;/Table Name[ ]*= STUDENT/{:a;N;/Table Reorg Information/!{$!ba}};x;/Table Reorg Information/{:a;N;/\n$/!{$!ba};p;}'
 Table Reorg Information:
   Reorg Type        =
        Reclustering
        Table Reorg
        Allow Read Access
        Recluster Via Table Scan
        Reorg Data Only
   Reorg Index       = 1
   Reorg Tablespace  = 2
   Start Time        = 09/28/2017 10:33:56.488234
   Reorg Phase       = 4 - Index Recreate
   Max Phase         = 4
   Phase Start Time  = 09/28/2017 10:33:57.463816
   Status            = Completed
   Current Counter   = 0
   Max Counter       = 0
   Completion        = 0
   End Time          = 09/28/2017 10:33:58.431537

命令的意义及解释参阅上一篇博文。

$!ba :末行不跳转至a,其他都跳转至a

sed之h;H和:a;N;ba使用精解(对段落进行操作)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值