简体   繁体   English

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

[英]awk append new line to end of line before with matching patterns

I have a file that contains the following: 我有一个包含以下内容的文件:

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)

As you can see, when the title of the track is too long, the title is appended on the next line, with TTITLE(samenumber)= in front. 如您所见,当曲目的标题过长时,标题会附加在下一行,前面是TTITLE(samenumber)= What i need to do is make these long titles one line. 我需要做的是使这些长标题成为一行。

My plan of attack was to identify the matching beginning of the lines, add a backslash to the end of the first of the two, use 我的攻击计划是确定各行的匹配开头,在这两行的末尾添加反斜杠,使用

cut -d"=" -f 2

to remove the 删除

TTITLE(num)=

then append the second line to the first using the famous awk one-liner 然后使用著名的awk单线将第二行附加到第一行

awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1'

Testing it out, if I manually add the backslashes and remove the TTITLE with cut , the awk statement works perfectly. 测试一下,如果我手动添加反斜杠并使用cut删除TTITLE ,则awk语句可以完美运行。 On the other hand, if someone has a better idea, please share! 另一方面,如果有人有更好的主意,请分享!

I would prefer using awk or sed because of the inability to install perl or ruby on the machines this will be running on, however, if this is the only solution, I can make it work. 我更喜欢使用awksed因为它无法在将要运行的机器上安装perlruby ,但是,如果这是唯一的解决方案,我可以使其工作。

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"}'

This awk will generate the output your are looking for 这个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) 

In case you need to keep TITLE: 如果您需要保留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"}'

and it 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) 

I believe all this can be done in awk itself. 我相信所有这些都可以在awk本身中完成。 Try this awk script: 试试这个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

For the above input file it gives: 对于上面的输入文件,它给出:

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)

Another way: 其他方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM