2017-09-04 16 views

回答

0

隨着GNU sed

sed ':a;N;/\n\([[:lower:]]\)/!ba;s// \1/' file 

Posixly正確的是:

#!/bin/bash 
sed ':a;N;/'$'\n''\([[:lower:]]\)/!ba;s// \1/' file 

由於\n未被POSIX定義爲轉義序列。

+0

GNU只有... – dawg

+0

的sed你認爲什麼是GNU的具體位置? – hek2mgl

+1

使用'\ n'。 POSIX'sed'不自由地支持'\ n'(具體來說,只作爲嵌入模式空間的文字)。用'gsed'和POSIX'sed'試過你的 - 只有'gsed'工作。否則,很好。 – dawg

-1

我想我想通了

enter code here`sed ':a;N;/\n[a-z]/s/\n/ /;ta;P;D' intput.txt > output.txt 
3

sed的是個人行簡單的換人,這是所有。你試圖做的不是一個簡單的替代,因此你不應該考慮使用sed。只需使用AWK:

$ awk '{printf "%s%s", (/^[[:upper:]]/?ors:OFS), $0; ors=ORS} END{print ""}' file 
Now is the time for all good men 
The quick brown fox jumped over 

以上將工作高效,便攜,強勁和任何UNIX系統上的任何AWK。

+0

這個似乎是一個更短的替代方案...'awk'{printf「%s%s」,$ 0,(/^[[:upper:]] /?OFS:ORS)}'' –

+0

這將打印尾隨空白字符,如果以大寫字母開頭的行之後沒有以較低字符開頭的行,則不會終止換行。 –

1

另一個awk

$ awk 'FNR>1 && /^[[:upper:]]/{print ""} {printf "%s%s", $0, OFS} END{print ""}' file 

並有perl

$ perl -ne 'print "\n" if $.>1 && /^[[:upper:]]/; s/\R/ /; print; END{print "\n"}' file 
相關問題