2011-05-16 120 views
0

我有一個文件,該文件包含以下內容: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' 

測試一下,如果我手動添加反斜槓並刪除TTITLEcutawk聲明完美地工作。另一方面,如果有人有更好的主意,請分享!

我寧願使用awksed因無力安裝在機器perlruby這將是上運行,但是,如果這是唯一的解決辦法,我可以使它發揮作用。

回答

2
awk -F"=" 'BEGIN {prev_title=""} {if ($1 == prev_title || NR ==1) { printf "%s", $2 } else { prev_title = $1; printf "\n%s", $2}} END {printf "\n"}' 

這awk將生成您正在尋找

Dispenser (Unreleased, 1995) 
Pivotal (From The Icebreaker 7", 1998) 
Sucker & Dry (From the Sucker & Dry 7", 1997) 
Icebreakers (From The Icebreaker 7", 1998) 
And The Bit Just Chokes Them (From the Sucker & Dry 7", 1997) 
There's A Coldest Day In Every Year (From The Disruption 7", 1996) 
A Disruption In The Normal Swing Of Things (From The Disruption 7", 1996) 
Nostalgia (From the Makoto Split 7" Series w/Small Brown Bike, 2001) 
The Knowledgeable Hasbeens (From The Disruption 7", 1996) 
Polar (From The Icebreaker 7", 1998) 
A Disruption In Our Lines Of Influence (From The Disruption 7", 1996) 
I Thought There'd Be More Than This (Unreleased, 1996) 

當輸出你需要保持TITLE:

awk -F"=" 'BEGIN {prev_title=""} {if ($1 == prev_title) { printf "%s", $2 } else { prev_title = $1; if (NR==1) {printf "%s", $0} else {printf "\n%s", $0}}} END {printf "\n"}' 

而且yeids

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", 1996) 
TTITLE6=A Disruption In The Normal Swing Of Things (From The Disruption 7", 1996) 
TTITLE7=Nostalgia (From the Makoto Split 7" Series w/Small Brown Bike, 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", 1996) 
TTITLE11=I Thought There'd Be More Than This (Unreleased, 1996) 
+0

這就是我所需要的,+1爲了超越第二個例子。 – rick 2011-05-16 04:53:41

+0

我不需要'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

1

我相信所有這些都可以在awk中完成。試試這個awk腳本:

awk -F '=' '{if (p==""){p=$1;line=$2} else if(p!=$1){print p "=" line; p=$1; line=$2} else if (p==$1) {line=line "\\\n" $2} } END{print p "=" line}' file 

對於上面的輸入文件時,它給出了:

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\ 
996) 
TTITLE6=A Disruption In The Normal Swing Of Things (From The Disruptio\ 
n 7", 1996) 
TTITLE7=Nostalgia (From the Makoto Split 7" Series w/Small Brown Bike,\ 
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\ 
", 1996) 
TTITLE11=I Thought There'd Be More Than This (Unreleased, 1996) 
+0

的感謝!這讓我有一半的方式,然後我能夠使用我的問題中的其他awk將我帶到那裏。再次感謝 – rick 2011-05-16 04:54:20

0

另一種方式:

awk -F= ' 
    {title[$1] = title[$1] $2} 
    END {for (id in title) print id "=" title[id]} 
' titles.txt | sort -V