2013-05-06 44 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