awk在匹配模式之前将新行追加到行尾

时间:2011-05-16 03:10:43

标签: bash scripting sed awk grep

我有一个包含以下内容的文件:

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的{​​{1}},cut语句就可以正常运行。另一方面,如果有人有更好的想法,请分享!

我希望使用awkawk,因为无法在将要运行的计算机上安装sedperl,但是,如果这是唯一的解决方案,我可以让它发挥作用。

3 个答案:

答案 0 :(得分: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"}'

并且它好了

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) 

答案 1 :(得分: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)

答案 2 :(得分:0)