免费注册 查看新帖 |

Chinaunix

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

[文本处理] 删除特定行以及替换特定行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-22 13:21 |只看该作者 |倒序浏览
文件名叫aaa.t
文件内容如下:

start
lsadf
3435w
A (0 0) (3 2)
   (3 7)

X adf23s
Y asdfj234w
sdf3
end

现在想把A开头和他下面一行换成:
B (9 9) (3 8)
  (9 9) (2 0)
以及删除X和Y开头那一行,请问怎么用awk写程序啊?
谢谢

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
2 [报告]
发表于 2013-01-22 14:02 |只看该作者
  1. awk '{if($0~/^A/){$0="B (9 9) (3 8)";line=NR+1;}}{if(line==NR)print "  (9 9) (2 0)"; else if($0!~/^X|Y/) print $0;}' c.txt
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-01-22 14:26 |只看该作者
回复 2# asdf2110


谢谢
如果想把A开头和他下面一行换成:
B (9 9) (3 8)
  (9 9) (2 0)
   (7 9) (7 )
程序应该怎么更新啊?

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
4 [报告]
发表于 2013-01-22 14:30 |只看该作者
回复 3# minzyyl
  1. awk '{if($0~/^A/){$0="B (9 9) (3 8)";line=NR+1;}}{if(line==NR)print "  (9 9) (2 0)\n  (7 9) (7 )"; else if($0!~/^X|Y/) print $0;}' c.txt
复制代码

论坛徽章:
0
5 [报告]
发表于 2013-01-22 14:36 |只看该作者
另外删除X,Y行后会在那行会生成“;”
能全部删除吗?

论坛徽章:
0
6 [报告]
发表于 2013-01-22 14:43 |只看该作者
回复 4# asdf2110


    另外删除X,Y行后会在那行会生成“;”
能全部删除吗?

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
7 [报告]
发表于 2013-01-22 14:45 |只看该作者
回复 5# minzyyl


   

论坛徽章:
7
数据库技术版块每日发帖之星
日期:2015-08-08 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-08-29 06:20:00数据库技术版块每日发帖之星
日期:2015-09-18 06:20:00数据库技术版块每周发帖之星
日期:2015-11-06 19:56:51数据库技术版块每日发帖之星
日期:2016-01-22 06:20:00数据库技术版块每日发帖之星
日期:2016-02-05 06:20:00
8 [报告]
发表于 2013-01-22 14:46 |只看该作者
回复 5# minzyyl
没有看见你说的分号“;”啊
  1. [root@localhost ~]$ cat c.txt
  2. start
  3. lsadf
  4. 3435w
  5. A (0 0) (3 2)
  6.    (3 7)

  7. X adf23s
  8. Y asdfj234w
  9. sdf3
  10. end
  11. [root@localhost ~]$ awk '{if($0~/^A/){$0="B (9 9) (3 8)";line=NR+1;}}{if(line==NR)print "  (9 9) (2 0)\n  (7 9) (7 )"; else if($0!~/^X|Y/) print $0;}' c.txt
  12. start
  13. lsadf
  14. 3435w
  15. B (9 9) (3 8)
  16.   (9 9) (2 0)
  17.   (7 9) (7 )

  18. sdf3
  19. end
  20. [root@localhost ~]$ awk --version
  21. awk --version
  22. GNU Awk 3.1.5
  23. Copyright (C) 1989, 1991-2005 Free Software Foundation.

  24. This program is free software; you can redistribute it and/or modify
  25. it under the terms of the GNU General Public License as published by
  26. the Free Software Foundation; either version 2 of the License, or
  27. (at your option) any later version.

  28. This program is distributed in the hope that it will be useful,
  29. but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  31. GNU General Public License for more details.

  32. You should have received a copy of the GNU General Public License
  33. along with this program; if not, write to the Free Software
  34. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
复制代码

论坛徽章:
0
9 [报告]
发表于 2013-01-22 14:47 |只看该作者
本帖最后由 minzyyl 于 2013-01-22 14:50 编辑
minzyyl 发表于 2013-01-22 14:43
回复 4# asdf2110


不好意思,是我看错了

论坛徽章:
0
10 [报告]
发表于 2013-01-22 14:48 |只看该作者
本帖最后由 minzyyl 于 2013-01-22 14:49 编辑

回复 8# asdf2110


    不好意思,是我看错了,原文本内容是:

X 234

Y 2safd

Z asdf
W sdf

askdjfsa
;
adf

我现在只想删除X到W所有的内容。删除后变成:
askdjfsa
;
adf
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP