awk 将新行附加到匹配模式之前的行尾

发布于 2024-11-07 02:34:36 字数 1600 浏览 0 评论 0原文

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

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

使用著名的 awk删除

TTITLE(num)=

然后将第二行附加到第一行one-liner

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

测试一下,如果我手动添加反斜杠并使用 cut 删除 TTITLE,则 awk 语句可以完美运行。另一方面,如果有人有更好的想法,请分享!

我更喜欢使用 awksed 因为无法在机器上安装 perlruby但是,如果这是唯一的解决方案,我可以让它发挥作用。

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. 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 '/\\$/ { 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味超标? 2024-11-14 02:34:36
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) 
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

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:

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) 
写下不归期 2024-11-14 02:34:36

我相信这一切都可以在 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)

I believe all this can be done in awk itself. Try this awk script:

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)
笑饮青盏花 2024-11-14 02:34:36

另一种方式:

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

Another way:

awk -F= '
  {title[$1] = title[$1] $2}
  END {for (id in title) print id "=" title[id]}
' titles.txt | sort -V
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文