2011-05-16 119 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