开发者

awk append new line to end of line before with matching patterns

开发者 https://www.devze.com 2023-03-06 15:19 出处:网络
I have a file that contains the following: TTITLE0=Dispenser (Unreleased, 1995) TTITLE1=Pivotal (From The Icebreaker 7\", 1998)

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 t开发者_Python百科he 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.


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) 


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)


Another way:

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

精彩评论

暂无评论...
验证码 换一张
取 消