2013-05-06 73 views
0

你好,我想解析这样的结构文件...名称后:{}它可以是具有相同形式的其他东西。某事:{}。如何解析文件中的一些文本在Perl中

--- 
NAMES: 
    first_thing: {} 

    second_thing: {} 

    name: {} 

感谢您的回答。

我想将它解析这种形式

first_thing 
second_thing 
name 
+3

这是不可能回答你的问题作为书面。例如,你不清楚你试图从文件中解析出什么,以及它应该如何构建。请澄清你的问题。 – 2013-05-06 06:40:14

+6

另外,告诉我们你尝试过的事情,或者说明你愿意为完成你的工作而支付的金额 – 2013-05-06 06:41:54

+1

我想删除前两行并用其他行删除括号和(:)冒号 – 2013-05-06 06:47:48

回答

1
perl -ne 'print "$1\n" if /^\s+ (\w+)/x' file 

在Windows上:

perl -ne "print qq{$1\n} if /^\s+ (\w+)/x" file 
+0

无法在-e行1处的EOF之前的任何位置找到字符串终止符“'”。 – 2013-05-06 08:38:29

+0

'my $ input =“input.txt”; my @myarray =''perl -ne“print qq {$ 1 \ n} if/^ \ s +(\ w +)/ x”$ input';' 感谢您的回答。我也可以问你,我怎样才能把它转换成另一个文件?结果myarray到output.txt例如 – 2013-05-06 11:03:29

+0

perl -ne“print qq {$ 1 \ n} if/^ \ s +(\ w +)/ x”file> output.txt – 2013-05-06 11:19:43

0

这可能有助于

preg_match_all('/^(\w+):\s(.*?)$/m', $input, $output, PREG_SET_ORDER); 
+0

未定义的子程序和main :: preg_match_all在test.pl第5行调用。 – 2013-05-06 08:39:19

+0

'#!/ usr/bin/perl my $ input =“$ input.txt”; my $ output =“$ output.txt”; (preg_match_all('/ ^(\ w +):\ s(。*?)$/m',$ input,$ output,PREG_SET_ORDER)){ print“SUCCESS”; } else { print“FAILED”; ($ *)= $ m/^(\ w +):\ s(。*?)$($ *)$' – 2013-05-06 08:40:19

+0

对不起,该函数是PHP的,我没有意识到你要求perl,无论如何试试这个在Perl中@output = /);' – 2013-05-06 08:45:28