免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4750 | 回复: 12
打印 上一主题 下一主题

[文本处理] awk/sed 按照段落截取关键字再筛选符合自己需要的字段 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-06-09 19:51 |只看该作者 |正序浏览
请问,如下字段,我该怎么截取出符合自己需要的数据呢 ?
System Database Directory

Number of entries in the directory = 17

Database 1 entry:

Database alias                       = ADtest
Database name                        = ADtest
Node name                            = EAMTNODE
Database release level               = c.00
Comment                              =
Directory entry type                 = Remote
Authentication                       = SERVER
Catalog database partition number    = -1
Alternate server hostname            =
Alternate server port number         =

Database 2 entry:

Database alias                       = Mdtest
Database name                        = Mdtest
Node name                            = BLDBNODE
Database release level               = d.00
Comment                              =
Directory entry type                 = Remote
Catalog database partition number    = -1
Alternate server hostname            =
Alternate server port number         =

Database 3 entry:

Database alias                       = Uatest
Database name                        = Uatest
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 4 entry:

Database alias                       = Cstest
Database name                        = Cstest
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              = Cognos DB
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 5 entry:

Database alias                       = WPtest
Database name                        = WPtest
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 6 entry:

Database alias                       = BADBOY
Database name                        = BADBOY
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 7 entry:

Database alias                       = USADSES
Database name                        = USADSES
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 8 entry:

Database alias                       = SPCPRD
Database name                        = SPCPRD
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 9 entry:

Database alias                       = PMPA
Database name                        = PMPDMPRD
Node name                            = PMPNODE
Database release level               = d.00
Comment                              =
Directory entry type                 = Remote
Catalog database partition number    = -1
Alternate server hostname            =
Alternate server port number         =

Database 10 entry:

Database alias                       = USADSTGE
Database name                        = USADSTGE
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 11 entry:

Database alias                       = JDBSS
Database name                        = JDBSS
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 12 entry:

Database alias                       = TEST
Database name                        = TEST
Local database directory             = /DB2/testserver
Database release level               = d.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 13 entry:

Database alias                       = AJDBDB
Database name                        = AJDBDB
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 14 entry:

Database alias                       = RELDB
Database name                        = RELDB
Node name                            = DB2INST6
Database release level               = d.00
Comment                              =
Directory entry type                 = Remote
Catalog database partition number    = -1
Alternate server hostname            =
Alternate server port number         =

Database 15 entry:

Database alias                       = PDBDMPR
Database name                        = PDBDMPR
Node name                            = DMNODE
Database release level               = d.00
Comment                              =
Directory entry type                 = Remote
Catalog database partition number    = -1
Alternate server hostname            =
Alternate server port number         =

Database 16 entry:

Database alias                       = Btest
Database name                        = Btest
Local database directory             =  /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =

Database 17 entry:

Database alias                       = CBMtest
Database name                        = CBMtest
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =
要求:共有17个段落,每个段落代表一个database的信息,现在我需要根据每个段落中的 “Directory entry type"  这一列的值来判断,如果这列值等于Indirect,则找出该段落的Database name的值,其余的跳过,请教CU shell牛人。。。

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
13 [报告]
发表于 2015-08-02 10:29 |只看该作者
回复 3# zxy877298415
你好,我有个地方不明白,
为什么改变RS后,\n成了FS


   

论坛徽章:
22
处女座
日期:2014-10-11 13:33:292015亚冠之塔什干火车头
日期:2015-07-20 19:59:042015亚冠之塔什干火车头
日期:2015-07-26 10:59:31程序设计版块每日发帖之星
日期:2015-08-05 06:20:00每日论坛发贴之星
日期:2015-08-05 06:20:00程序设计版块每日发帖之星
日期:2015-08-07 06:20:00每日论坛发贴之星
日期:2015-08-07 06:20:002015亚冠之阿尔纳斯尔
日期:2015-10-01 15:23:28白银圣斗士
日期:2015-12-07 17:17:06操作系统版块每日发帖之星
日期:2015-12-27 06:20:002015亚冠之广州富力
日期:2015-07-08 15:48:31程序设计版块每日发帖之星
日期:2015-06-11 22:20:00
12 [报告]
发表于 2015-08-02 10:27 |只看该作者
练习
awk -F= '/Database name/{a=$2;c++}/Directory entry type/{if($2==" Indirect")print "the"c"\x27s database name is:"a}' file
the3's database name is: Uatest
the4's database name is: Cstest
the5's database name is: WPtest
the6's database name is: BADBOY
the7's database name is: USADSES
the8's database name is: SPCPRD
the10's database name is: USADSTGE
the11's database name is: JDBSS
the12's database name is: TEST
the13's database name is: AJDBDB
the16's database name is: Btest
the17's database name is: CBMtest

论坛徽章:
7
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:18程序设计版块每日发帖之星
日期:2015-08-09 06:20:00每日论坛发贴之星
日期:2015-08-09 06:20:00程序设计版块每日发帖之星
日期:2015-08-22 06:20:00程序设计版块每日发帖之星
日期:2015-08-27 06:20:00
11 [报告]
发表于 2015-07-31 10:20 |只看该作者
本帖最后由 tuyajie 于 2015-07-31 10:25 编辑
  1. [jay.liu@aliCloud ~]$ egrep "Database name|Directory entry type" testfile  | grep -B1 "Indirect" | grep "Database name"
  2. Database name                        = Uatest
  3. Database name                        = Cstest
  4. Database name                        = WPtest
  5. Database name                        = BADBOY
  6. Database name                        = USADSES
  7. Database name                        = SPCPRD
  8. Database name                        = USADSTGE
  9. Database name                        = JDBSS
  10. Database name                        = TEST
  11. Database name                        = AJDBDB
  12. Database name                        = Btest
  13. Database name                        = CBMtest
复制代码
无聊写一个更无聊的

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
10 [报告]
发表于 2015-07-30 23:07 |只看该作者
What You Want Is What You Code(WYWIWYC)

Database 17 entry:

Database alias                       = CBMtest
Database name                        = CBMtest
Local database directory             = /DB2/testserver
Database release level               = f.00
Comment                              =
Directory entry type                 = Indirect
Catalog database partition number    = 0
Alternate server hostname            =
Alternate server port number         =


>> ... 段落中的 “Directory entry type"  这一列的值来判断,如果这列值等于Indirect,则找出该段落的Database name的值,其余的跳过

$ awk -F'[\t ]*=[\t ]*' '/Database name/{dn=$2}/Directory entry type/&&$2=="Indirect"{print dn}' FILE
Uatest
Cstest
WPtest
BADBOY
USADSES
SPCPRD
USADSTGE
JDBSS
TEST
AJDBDB
Btest
CBMtest

论坛徽章:
2
巳蛇
日期:2014-06-02 13:33:59午马
日期:2015-01-11 00:11:16
9 [报告]
发表于 2014-06-11 13:24 |只看该作者
本帖最后由 shou55 于 2014-06-11 13:33 编辑
  1. awk -F "=" '/Database name/{s=$2}; /Directory entry type                 = Indirect/{print s;}'
复制代码

论坛徽章:
0
8 [报告]
发表于 2014-06-10 20:26 |只看该作者
回复 5# LikeLx


    阁下的语句通俗易用,感谢

论坛徽章:
3
酉鸡
日期:2014-04-23 17:17:56丑牛
日期:2014-05-08 11:41:302015亚冠之德黑兰石油
日期:2015-08-03 11:14:27
7 [报告]
发表于 2014-06-10 09:17 |只看该作者
回复 6# yestreenstars


    膜拜星辰兄,sed的高级命令一直用不好~

论坛徽章:
32
处女座
日期:2013-11-20 23:41:20双子座
日期:2014-06-11 17:20:43戌狗
日期:2014-06-16 11:05:00处女座
日期:2014-07-22 17:30:47狮子座
日期:2014-07-28 15:38:17金牛座
日期:2014-08-05 16:34:01亥猪
日期:2014-08-18 13:34:25白羊座
日期:2014-09-02 15:03:55金牛座
日期:2014-11-10 10:23:58处女座
日期:2014-12-02 09:17:52程序设计版块每日发帖之星
日期:2015-06-16 22:20:002015亚冠之塔什干火车头
日期:2015-06-20 23:28:22
6 [报告]
发表于 2014-06-10 09:07 |只看该作者
  1. awk '/^Database name\>/{t=$NF}/^Directory entry type\>.*\<Indirect$/{print t}'
复制代码
  1. sed '/^Database name\b/{s/.* //;h};/^Directory entry type\b.*\bIndirect$/{g;p};d'
复制代码

论坛徽章:
36
摩羯座
日期:2013-09-23 16:37:312015年亚洲杯之沙特阿拉伯
日期:2015-04-14 09:10:172015亚冠之柏太阳神
日期:2015-06-25 08:48:212015亚冠之武里南联
日期:2015-07-28 09:01:082015亚冠之莱赫维亚
日期:2015-07-28 15:44:172015亚冠之柏斯波利斯
日期:2015-09-06 14:08:52白银圣斗士
日期:2015-11-25 17:06:2815-16赛季CBA联赛之吉林
日期:2015-12-09 16:59:072016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之辽宁
日期:2016-04-14 09:29:04luobin
日期:2016-06-17 17:46:3615-16赛季CBA联赛之天津
日期:2016-08-16 14:11:01
5 [报告]
发表于 2014-06-10 09:05 |只看该作者
本帖最后由 LikeLx 于 2014-06-10 09:44 编辑
  1. awk -F '=' '/Database name/{s=$2;++n;next}/Directory entry type/{if($2==" Indirect") print "Databases"n s}'
复制代码
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP