我有一個約5000行的文本文件。我想刪除在小寫字符前面的線條,讓如何在sed中刪除小寫字母前面的換行符?
Now is the time for
all good men
The quick brown fox
jumped over
變爲:
Now is the time for all good men
The quick brown fox jumped over
我有一個約5000行的文本文件。我想刪除在小寫字符前面的線條,讓如何在sed中刪除小寫字母前面的換行符?
Now is the time for
all good men
The quick brown fox
jumped over
變爲:
Now is the time for all good men
The quick brown fox jumped over
隨着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定義爲轉義序列。
我想我想通了
enter code here`sed ':a;N;/\n[a-z]/s/\n/ /;ta;P;D' intput.txt > output.txt
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。
這個似乎是一個更短的替代方案...'awk'{printf「%s%s」,$ 0,(/^[[:upper:]] /?OFS:ORS)}'' –
這將打印尾隨空白字符,如果以大寫字母開頭的行之後沒有以較低字符開頭的行,則不會終止換行。 –
另一個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
GNU只有... – dawg
的sed你認爲什麼是GNU的具體位置? – hek2mgl
使用'\ n'。 POSIX'sed'不自由地支持'\ n'(具體來說,只作爲嵌入模式空間的文字)。用'gsed'和POSIX'sed'試過你的 - 只有'gsed'工作。否則,很好。 – dawg