2012-01-31 17 views
1

格式文本我對這個模板的文本:用Perl

In 1935 he was asked to document the principal dancers and productions and 
George newly . 

he continued to shoot fashion 
Bergdorf Goodman and Saks Fifth 
started a series of photographs . 

,並希望每一個段落轉換爲由seprated一線「\ n」,即輸出將是:

In 1935 he was asked to document the principal dancers and productions George newly . 

he continued to shoot fashion Bergdorf Goodman and Saks Fifth started a series of photographs . 

如何用perl格式化這樣的東西?有人可以提供一個例子嗎?

我試圖用文字::裹像下面卻得到不想要的結果

$Text::Wrap::separator=' '; 
my $text=fill("","",$text); 
+0

要使用'文本實現這一:: Wrap'你必須先單獨段落('拆分 「\ n \ n」,$ text'),然後換行每個數組元素。 – dgw 2012-01-31 15:38:12

+0

你的用途是什麼?只是改變一個文本文件?修復腳本中的字符串/文件句柄? – TLP 2012-01-31 16:50:34

+0

http://p3rl.org/Text::Autoformat – daxim 2012-01-31 17:55:02

回答

2

對於一個內膽,你可以嘗試這樣的事:

perl -00 -l -pwe 's/\n//g' foo/george.txt 

-00將設置輸入記錄分隔符$/""並激活段落模式。 -l將設置輸出記錄分隔符$\"\n\n"(在這種情況下)。

在腳本版本:

$/ = ""; 
$\ = "\n\n"; 
while (<>) { 
    chomp; 
    s/\n//g; 
    print; 
} 
+0

+1。好的解決方案 – flesk 2012-02-01 06:41:08

0
#!/usr/bin/perl 
use strict; 
use warnings; 

$/=""; #lines are paragraphs - perlfaq5 
#files are command line args, or STDIN 
while(<>){s/\n//g; print $_,"\n\n";} 
+0

tr/\ n // d;比這裏的s /// g更好(因爲我們正在處理的是字符而不是模式)。 – tadmc 2012-01-31 16:18:42

+0

如果你正在設置'$ /',那麼也可以設置'$ \'。 – TLP 2012-01-31 16:32:30

+0

這兩個說法都是真的。 tr沒有出現在我身上,我從來沒有想過$ \ ... – 2012-01-31 16:57:01

2

您可以用文字::裹做,但一),你需要閱讀的文件在一個段落時間和b)你需要設置一個人爲的高右邊距。

#!/usr/bin/perl 

use strict; 
use warnings; 

use Text::Wrap; 

$Text::Wrap::columns = 10_000; 
local $/ = ''; # Always localise changes to $/ 

while (<DATA>) { 
    print fill('', '', $_), "\n\n"; 
} 

__DATA__ 
In 1935 he was asked to document the principal dancers and productions and 
George newly . 

he continued to shoot fashion 
Bergdorf Goodman and Saks Fifth 
started a series of photographs .