免费注册 查看新帖 |

Chinaunix

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

[文本处理] sed 匹配后隔30行插入内容的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-20 18:48 |只看该作者 |倒序浏览
本帖最后由 chen_7733 于 2014-11-20 19:09 编辑

sed -e '/r100002/{n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;r aaa.txt' -e '}' bbb.txt

这个sed 能正常打印.但是无法修改bbb.txt中,这个shell能修改成写入到bbb.txt 吗或者更好的命令,这段代码是在匹配行r100002后30行插入aaa.txt的内容
---------------------
aaa.txt
1111111
2222222

bbb.txt
r100002
aaa
ddd
dddd
eee
ddd
......
ee
eeee

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
2 [报告]
发表于 2014-11-20 19:19 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2014-11-20 19:25 |只看该作者
所以问问大牛还有没有其他好的代码表达方式

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2014-11-20 21:28 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
5 [报告]
发表于 2014-11-20 22:21 |只看该作者
回复 1# chen_7733
  1. sed -e '/xxx/{:a;x;/^\.\{30\}/{x;r yyy' -e 'b};s/^/./;x;n;ba};' file
复制代码

论坛徽章:
9
2015亚冠之大阪钢巴
日期:2015-06-04 11:47:30丑牛
日期:2015-01-22 15:49:26巳蛇
日期:2015-01-22 10:11:18巨蟹座
日期:2014-11-20 10:55:03天蝎座
日期:2014-11-16 22:10:26处女座
日期:2014-11-16 11:01:10申猴
日期:2014-09-19 11:12:37双鱼座
日期:2014-07-25 10:09:54程序设计版块每日发帖之星
日期:2015-08-24 06:20:00
6 [报告]
发表于 2014-11-20 22:55 |只看该作者
学些了, 已收藏

论坛徽章:
0
7 [报告]
发表于 2014-11-21 08:24 |只看该作者
本帖最后由 chen_7733 于 2014-11-21 08:30 编辑

1.yinyuemi 老大的代码比我的简洁了许多,但是插入行还是无法写入到bbb.txt 文件中
2.我的需求是这样的
[root@appbi shellchen]# cat aaa.txt
<fsg:RptCell ColCnt="c1112" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1113" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1114" RealNum="0.000000">0.00</fsg:RptCell>
[root@appbi shellchen]# cat bbb.txt
<fsg:RptLine RptCnt="p1001" RowCnt="r100002" LineRowSeq="              20.0000000000000" LinCnt="l100002">
<fsg:RptCell ColCnt="c1000" RealDesc="  1.1广告代理净收入">  1.1广告代理净收入                </fsg:RptCell>
<fsg:RptCell ColCnt="c1001" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1002" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1003" RealNum="62860974.040000">62,860,974.04</fsg:RptCell>
<fsg:RptCell ColCnt="c1004" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1005" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1006" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1007" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1008" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1009" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1010" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1011" RealNum="-866037.740000">-866,037.74</fsg:RptCell>
<fsg:RptCell ColCnt="c1012" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1013" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1014" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1015" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1016" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1017" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1018" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1019" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1020" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1021" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1022" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1023" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1024" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1025" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1026" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1027" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1028" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1029" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1030" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1031" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1032" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1033" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1034" RealNum="-146249764.000000">-146,249,764.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1035" RealNum="-84254827.700000">-84,254,827.70</fsg:RptCell>


最终的结果是把aaa.txt 文件中的三行插入到bbb.txt中,条件是匹配行RowCnt="r100002" 后30行的位置
结果:
.....
<fsg:RptCell ColCnt="c1023" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1024" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1025" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1026" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1027" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1028" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1029" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1112" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1113" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1114" RealNum="0.000000">0.00</fsg:RptCell>

<fsg:RptCell ColCnt="c1030" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1031" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1032" RealNum="0.000000">0.00</fsg:RptCell>
<fsg:RptCell ColCnt="c1033" RealNum="0.000000">0.00</fsg:RptCell>
.........

其实最后加个>输入也可以实现,但是我要对通一个文件(bbb.txt)循环操作20多次,所以就不能用定向输出了.

论坛徽章:
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
8 [报告]
发表于 2014-11-21 08:43 |只看该作者
回复 1# chen_7733

1. for check only
$ perl -lape 'BEGIN{chomp($a=`cat aaa.txt`)}{if($p){if($c++==3){print $a}};/r100002/&&{$p=1,$c=0}}' bbb.txt
r100002
aaa
ddd
dddd
1111111
2222222

eee
ddd

2. replace the bbb.txt after check without issue
$ perl -i -lape 'BEGIN{chomp($a=`cat aaa.txt`)}{if($p){if($c++==30){print $a}};/r100002/&&{$p=1,$c=0}}' bbb.txt

论坛徽章:
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
9 [报告]
发表于 2014-11-21 09:44 |只看该作者
回复 7# chen_7733

加上-i参数
   

论坛徽章:
0
10 [报告]
发表于 2014-11-21 09:48 |只看该作者
回复 9# yestreenstars
[root@appbi shellchen]# sed -i '/RowCnt="r100002"/{n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;r aaa.txt' -e '}' bbb.txt           
sed: -e expression #1, char 1: unexpected `}'


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP