我有一個文件,該文件包含以下內容:awk的前追加新行結束線的匹配模式
TTITLE0=Dispenser (Unreleased, 1995)
TTITLE1=Pivotal (From The Icebreaker 7", 1998)
TTITLE2=Sucker & Dry (From the Sucker & Dry 7", 1997)
TTITLE3=Icebreakers (From The Icebreaker 7", 1998)
TTITLE4=And The Bit Just Chokes Them (From the Sucker & Dry 7", 1997)
TTITLE5=There's A Coldest Day In Every Year (From The Disruption 7", 1
TTITLE5=996)
TTITLE6=A Disruption In The Normal Swing Of Things (From The Disruptio
TTITLE6=n 7", 1996)
TTITLE7=Nostalgia (From the Makoto Split 7" Series w/Small Brown Bike,
TTITLE7= 2001)
TTITLE8=The Knowledgeable Hasbeens (From The Disruption 7", 1996)
TTITLE9=Polar (From The Icebreaker 7", 1998)
TTITLE10=A Disruption In Our Lines Of Influence (From The Disruption 7
TTITLE10=", 1996)
TTITLE11=I Thought There'd Be More Than This (Unreleased, 1996)
正如你可以看到,當曲目的標題過長,標題附加在下一行,前面有TTITLE(samenumber)=
。我需要做的是使這些長期的標題一行。
我的進攻計劃是確定開始的行的匹配,增加一個反斜槓第一兩個結束,使用
cut -d"=" -f 2
刪除
TTITLE(num)=
然後將第二行添加到第一行使用着名的awk
單線程
awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1'
測試一下,如果我手動添加反斜槓並刪除TTITLE
與cut
,awk
聲明完美地工作。另一方面,如果有人有更好的主意,請分享!
我寧願使用awk
或sed
因無力安裝在機器perl
或ruby
這將是上運行,但是,如果這是唯一的解決辦法,我可以使它發揮作用。
這就是我所需要的,+1爲了超越第二個例子。 – rick 2011-05-16 04:53:41
我不需要'BEGIN'部分,因爲在'awk'中,未初始化的變量將被解析爲''「'無論如何:'awk -F」=「'$ 1 == prev_title {printf」%s「,$ 2;下一個} {prev_title = $ 1} NR == 1 {printf「%s」,$ 0; next} {printf「\ n%s」,$ 0} END {print「」}'' ' – mschilli 2013-09-02 13:54:27