使用perl将文本文件分成几个块

时间:2014-08-19 08:22:06

标签: regex perl

我有一个包含diff输出的文件,如下所示:

26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 
34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 
43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 
49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song

我想逐块处理这个文件,其中twho块由表格的分隔符填充:

{number} c {number}例如 48c70

{number},{number} c {number}例如 90,120c70

{number},{number} c {number},{number}例如 36,40c88,1020

{number} c {number},{number}例如 99,100c70

例如,上面的文件包含4个块:

第1块:

26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 

第2块:

34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 

第3块:

43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 

第4块:

49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song

我怎么能用Perl做到这一点?

谢谢

2 个答案:

答案 0 :(得分:0)

根据

拆分字符串
(?=^(\d+,)?\d+c\d+(,\d+)?$)

这是online demo

答案 1 :(得分:0)

由于您只处理来自diff的输出,您只需要查找以数字开头的行:

my @blocks = split /^(?=\d)/m, do {local $/; <DATA>};

use Data::Dump;
dd @blocks;

__DATA__
26c26
< Or in the bright light of morning 
---
> Or in the bright light of evning 
34c34,36
< clouds will roll back clear of the summits. 
---
> hello world
> I came here to ask
> some questions 
43,44c45
< How to imagine an orchid boat? 
< It gets harder. But days come and go, 
---
> don't you cry tonigh, 
49,50c50,52
< though only caught that imprisoning moment. 
< A golden leaf in autumn spins into a dark river 
---
> che che che che changes. 
> la la la la la 
> good song

输出:

(
  "26c26\n< Or in the bright light of morning \n---\n> Or in the bright light of evning \n",
  "34c34,36\n< clouds will roll back clear of the summits. \n---\n> hello world\n> I came here to ask\n> some questions \n",
  "43,44c45\n< How to imagine an orchid boat? \n< It gets harder. But days come and go, \n---\n> don't you cry tonigh, \n",
  "49,50c50,52\n< though only caught that imprisoning moment. \n< A golden leaf in autumn spins into a dark river \n---\n> che che che che changes. \n> la la la la la \n> good song",
)