免费注册 查看新帖 |

Chinaunix

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

[文本处理] 文件提取 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-10 17:38 |只看该作者 |倒序浏览
如何循环提取文本中指定行数的数据,例如我要提取7~26行,36~55行的数据,存在一个文件中。示例数据如下所示:

mem=211456MB

faults
------------
fre
854219
853292
854156
853431
853608
854036
854002
853996
854063
854106
853488
854670
854307
854535
854310
854571
854018
853713
854411
854217
VALUE:92
VALUE:


mem=211456MB

faults
------------
fre
851406
850971
851362
851680
853297
853432
853437
853337
853265
852620
853192
852603
852780
852999
850838
854277
853719
854340
854265
854282
VALUE:93.3684
VALUE:


mem=211456MB

faults
------------
fre
852836
852788
852710
852816
852680
852659
852739
852412
852511
852272
852470
852481
852726
852508
852651
852260
852861
852833
852871
852148
VALUE:93.4211
VALUE:

论坛徽章:
0
2 [报告]
发表于 2012-08-10 17:42 |只看该作者
  1. sed -n '7,26p;36,55p' file
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
3 [报告]
发表于 2012-08-10 17:49 |只看该作者
You can try split or csplit command.

论坛徽章:
0
4 [报告]
发表于 2012-08-10 21:12 |只看该作者
awk 'NR>=7&&NR<=26||NR>=36&&NR<=55{print}'  file

论坛徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亚冠之浦和红钻
日期:2015-06-29 15:30:48
5 [报告]
发表于 2012-08-10 23:41 |只看该作者
  1. grep -Po '^[0-9]{6}我觉得这个方法更方便
复制代码
我觉得这个方法更方便

论坛徽章:
0
6 [报告]
发表于 2012-08-10 23:50 |只看该作者
谢谢大家的回复,我的最终目标是将7~26行,36~55行做为变量,将其存在一个文件中,如下所示:
854219  851406   
853292  850971
854156  851362
853431  851680
853608  853297
854036  853432
854002  853437
853996  853337
854063  853265
854106  852620
853488  853192
854670  852603
854307  852780
854535  852999
854310  850838
854571  854277
854018  853719
853713  854340
854411  854265
854217  854282

目前已经做到将其转换为下面形式,接下来要做的是每20行提取出来到一个文件中,以期达到上面形式:

854219
853292
854156
853431
853608
854036
854002
853996
854063
854106
853488
854670
854307
854535
854310
854571
854018
853713
854411
854217
851406
850971
851362
851680
853297
853432
853437
853337
853265
852620
853192
852603
852780
852999
850838
854277
853719
854340
854265
854282

论坛徽章:
0
7 [报告]
发表于 2012-08-11 08:48 |只看该作者
回复 6# guozl2118


指定行号的话,上面sed,awk都给出来了。。这边man一下split和paste

split 命令,可按行拆分文件

    SPLIT(1)                         User Commands                        SPLIT(1)

NAME
       split - split a file into pieces

SYNOPSIS
       split [OPTION]... [INPUT [PREFIX]]

DESCRIPTION
       Output  fixed-size  pieces of INPUT to PREFIXaa, PREFIXab, ...; default
       size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or  when
       INPUT is -, read standard input.

-l, --lines=NUMBER
              put NUMBER lines per output file



paste 可按列合并文件

PASTE(1)                         User Commands                        PASTE(1)

NAME
       paste - merge lines of files

SYNOPSIS
       paste [OPTION]... [FILE]...

DESCRIPTION
       Write  lines  consisting  of  the sequentially corresponding lines from
       each FILE, separated by TABs, to standard output.   With  no  FILE,  or
       when FILE is -, read standard input.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.
  1. personball@vostro:~$cat test1
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. personball@vostro:~$
  13. personball@vostro:~$split -l5 test1
  14. personball@vostro:~$ls
  15. test1        xaa    xab
  16. personball@vostro:~$cat xaa
  17. 1
  18. 2
  19. 3
  20. 4
  21. 5
  22. personball@vostro:~$cat xab
  23. 6
  24. 7
  25. 8
  26. 9
  27. 10
  28. personball@vostro:~$paste xaa xab
  29. 1        6
  30. 2        7
  31. 3        8
  32. 4        9
  33. 5        10
  34. personball@vostro:~$
复制代码

论坛徽章:
0
8 [报告]
发表于 2012-08-16 06:07 |只看该作者
使用head跟tail结合可以截取文件中任意范围的行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP