2013-07-23 141 views
0

我有一个快速的问题..多行匹配PERL

我想匹配一个特定的多线程实例。问题是,当我执行我的代码时,它只打印我编辑的内容,而不是整个文件。

例如。这是我输入:

JJJ 
1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 

我的目标是获得:

JJJ 1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 

所以基本上我只是想与JJJ或1倍或更大的资本任何其他变化来发出声音的数据到行字母。

然而,当我这样做,我只得到这样的:

JJJ 1234   123.00  1234.28    123456.00  1234567.72 constant 

我只得到这一点,只是,而不是其他的信息,我需要的文件中。我知道有一个简单的解决方案,但我是perl非常新,并不能完全弄清楚。

这是我的代码,也许你们中的一些人会有建议。

use File::Slurp; 
my $text = read_file('posf.txt'); 
while ($text =~ /(^[A-Z]+)(\d+.*?\.\d+ Acquired$)/gism) { 
$captured = $1." ".$2; 
$captured =~ s/\n//gi; 

print $captured."\n"; 
} 

任何帮助将是伟大的。我知道我只是告诉程序打印“抓取”,但我无法弄清楚如何让它打印文件的其余部分,并将线路放到所需的位置。

我希望我的问题有意义,不难理解,请告知我是否可以进一步查询。

+0

你能写出尽可能最小的例子来重现你的错误吗?目前,您在正则表达式中使用单词“Acquired”,而您的数据中缺少该单词。 – user4035

+0

即时通讯对不起。....让我重新发布代码..我打算切换到常数.. – joshE

+0

使用File :: Slurp; my $ text = read_file('posf.txt'); ($ text =〜/(^[A-Z]+)(dd+.*?\.d + constant $)/ gism){$ text =〜1。“”。$ 2; $($ text =〜/(^[-)) $ captured =〜s/\ n // gi; print $ captured。“\ n”; } – joshE

回答

0

希望我能正确理解你的问题:你想在文本中的每行之后删除换行符,只包含大写字母。试试这个代码:

#!/usr/bin/perl 

use strict; 
use warnings; 

my $text = qq{JJJ 
1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 
JJJ 
1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 
}; 

$text =~ s/(^[A-Z]+) #if the line starts with at least 1 capital letter 
     \r?   #followed by optional \r - for DOS files 
     \n$/   #followed by \n 
     $1 /mg;  #replace it with the 1-st group and a space 
print $text; 

它打印:

JJJ 1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 
JJJ 1234   123.00  1234.28    123456.00  1234567.72 constant 
ld;afksd;l REst of file blah blah blah...rest of file and other info I neeed etc. 

我没有从文件中读取文本显示测试数据。但您可以轻松地添加read_file呼叫。

+0

谢谢..我只是需要弄清楚如何使用这个模块,而不会丢失我的文件的其余部分...似乎s /诀窍!谢谢! – joshE