2009-12-24 50 views
3

是否有一個?Unix「包裝」過濾器

東西我可以用這樣的:

$ cat someFileWithLongLines.txt | wrap -80 --indent|less 
+1

感謝您的答案之前,在這裏張貼我做了「man -k wrap」,我完全錯過了'fold':( – atrent 2009-12-24 14:58:11

+2

現在我們只需要'spindle'和'mutilate'。 – sdg 2009-12-24 16:00:40

+0

@sdg:這裏是mutilate:P 'echo'hello world'| sed - e's/\(。\)/ \ 1 \ n/g'| shuf | paste -s -d''' – Hasturkun 2009-12-24 16:42:23

回答

1

的命令卻是「折」,但它不支持縮進線的纏繞部分。你需要爲那個破解awk。

6

您可能需要fold命令。

$ fold -w 80 file.txt 

$ cat file.txt | fold 
1

該命令被稱爲摺疊。

$ cat someFileWithLongLines.txt |倍

0

您可以使用AWK

width=10 
awk -vw="$width" '{ 
    i=1 
    while(length(substr($0,i,w))){ 
     print substr($0,i,w) 
     i+=w 
    } 
}' file 

輸出:

$ more file 
this is a line 1 
this is a line 2 
$ fold -w 10 file 
this is a 
line 1 
this is a 
line 2 
$ ./shell.sh 
this is a 
line 1 
this is a 
line 2 
2

您可以pr縮進,如果你喜歡,例如。

$ fold -w 76 -s file.txt | pr -T --indent=4 
11

GNU的coreutils有一個名爲fmt命令:

$ fmt -40 -t lorem 
 
Lorem ipsum dolor sit amet, consectetur 
    adipisicing elit, sed do eiusmod 
    tempor incididunt ut labore et 
    dolore magna aliqua. Ut enim 
    ad minim veniam, quis nostrud 
    exercitation ullamco laboris 
    nisi ut aliquip ex ea commodo 
    consequat. Duis aute irure dolor 
    in reprehenderit in voluptate velit 
    esse cillum dolore eu fugiat nulla 
    pariatur. Excepteur sint occaecat 
    cupidatat non proident, sunt in 
    culpa qui officia deserunt mollit 
    anim id est laborum. 

編輯:正如你所看到的,在給定寬度內字邊界fmt斷裂線。將其與fold的硬邊界對比。該類型縮進該fmt的也可能不是你要找的東西,但你可以把它管道(不-t選項)通過pr獲得保證金風格的縮進:

fmt -40 lorem | pr -To 6 
+2

fold也h作爲'-s'選項,而不是空格。 – Hasturkun 2009-12-24 16:27:21

+0

謝謝,我錯過了。 – 2009-12-24 19:59:35