免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1683 | 回复: 4

[文本处理] sed如何提取两段格式相同的文本(已经解决) [复制链接]

论坛徽章:
0
发表于 2015-05-11 16:03 |显示全部楼层
本帖最后由 la19850302 于 2015-05-11 17:05 编辑

文本格式如下 create.sql


Table structure for table table_name_1
sssssxxx
Table structure for table table_name_1

Table structure for table table_name_2
sssssxx
Table structure for table table_name_2

Table structure for table table_name_3
sssssx
Table structure for table table_name_3

.....同上类推

Table structure for table table_name_100
sssssxxx
Table structure for table table_name_100

比如我想取 table_name_30 和 table_name_1,最终结果如下
Table structure for table table_name_1
sssssxxx
Table structure for table table_name_1
Table structure for table table_name_31
sssssxxx
Table structure for table table_name_31

必须写两次 sed(如下) ,对于大文件来说,create.sql 可能是好几十个G ,时间成本是单次提取的两倍,有没有办法提高提取效率,一次性提取出来

sed -n 's/Table structure for table table_name_1/,/Table structure for table table_name_1/p' create.sql  > table_name_1.sql

sed -n  's/Table structure for table table_name_31/,/Table structure for table table_name_31/p' create.sql  > table_name_31.sql

谢谢大家




论坛徽章:
1
2015亚冠之鹿岛鹿角
日期:2015-05-12 13:04:44
发表于 2015-05-11 16:07 |显示全部楼层
  1. sed -n '/Table structure for table table_name_1$/,/Table structure for table table_name_1$/p;/Table structure for table table_name_31$/,/Table structure for table table_name_31$/p' create.sql
复制代码

论坛徽章:
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
发表于 2015-05-11 16:17 |显示全部楼层
回复 1# la19850302

$ awk -F_ -vs="1 31" 'BEGIN{t=split(s,a," ");for(n=1;n<=t;n++)b[a[n]]=1}b[$NF]{p=!p;print;next}p' FILE
Table structure for table table_name_1
sssssxxx
Table structure for table table_name_1
Table structure for table table_name_31
sssssx
Table structure for table table_name_31

   

论坛徽章:
0
发表于 2015-05-11 16:32 |显示全部楼层
这个也可以的,牛逼啊,我几年没来论坛问问题了,这么多热血少年,感动哭了
回复 3# jason680


   

论坛徽章:
0
发表于 2015-05-11 16:33 |显示全部楼层
谢谢,可以的,论坛高手如云啊
回复 2# A_Traveler


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP