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
使用著名的 awk
删除
TTITLE(num)=
然后将第二行附加到第一行one-liner
awk '/\\$/ { sub(/\\$/,""); getline t; print $0 t; next }; 1'
测试一下,如果我手动添加反斜杠并使用 cut
删除 TTITLE
,则 awk
语句可以完美运行。另一方面,如果有人有更好的想法,请分享!
我更喜欢使用 awk
或 sed
因为无法在机器上安装 perl
或 ruby
但是,如果这是唯一的解决方案,我可以让它发挥作用。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个 awk 将生成您正在寻找的输出,
以防您需要保留 TITLE:
,它是的
This awk will generate the output your are looking for
In case you need to keep TITLE:
and it yeids
我相信这一切都可以在 awk 本身中完成。尝试这个 awk 脚本:
对于上面的输入文件,它给出:
I believe all this can be done in awk itself. Try this awk script:
For the above input file it gives:
另一种方式:
Another way: