2

I have the following sample:

1. TNT 00:00 2. Swing From The Gutters 7:34 3. Ten-Day Interval 13:29 4. I Set My Face To The Hillside 18:16 5. The Equator 24:26 6. A Simple Way To Go Faster Than Light That Does Not Work 28:27 7. The Suspension Bridge At Iguazu Falls 32:03 8. Four-Day Interval 37:42 9. In Sarah, Mencken, Christ And Beethoven There Were Women And Men 42:29 10. Almost Always Is Nearly Enough 50:01 11. Jetty 52:45 12. Everglade 1:01:09 1:05:32

Which I want to convert into:

1. TNT 00:00 
2. Swing From The Gutters 7:34 
3. Ten-Day Interval 13:29 
4. I Set My Face To The Hillside 18:16 
5. The Equator 24:26 
6. A Simple Way To Go Faster Than Light That Does Not Work 28:27 
7. The Suspension Bridge At Iguazu Falls 32:03 
8. Four-Day Interval 37:42 
9. In Sarah, Mencken, Christ And Beethoven There Were Women And Men 42:29 
10. Almost Always Is Nearly Enough 50:01 
11. Jetty 52:45 
12. Everglade 1:01:09 1:05:32

The logical path I was following is that I know there will always be a "dot" (.) that comes with the number of the track and I can use it as a guideline for the breaks. The trouble is, it has to crawl back one or two characters in order to keep the number of the tracks.

4

At least with GNU sed, you can insert a newline before all but the first matching sequence using a 2g modifier:

sed -E 's/[0-9]+\./\n&/2g' file
  • 1
    The Ng means "replace globally but start at the Nth occurrence", right? – terdon Dec 8 '18 at 18:43
  • @terdon yes that's right - or as the manual puts it, "ignore matches before the numberth, and then match and replace all matches from the numberth on" – steeldriver Dec 8 '18 at 20:39
4

Just capture the number and the dot and insert a newline before them:

$ perl -pe 's/\s(\d+\.)/\n$1/g' file 
1. TNT 00:00 
2. Swing From The Gutters 7:34 
3. Ten-Day Interval 13:29 
4. I Set My Face To The Hillside 18:16 
5. The Equator 24:26 
6. A Simple Way To Go Faster Than Light That Does Not Work 28:27 
7. The Suspension Bridge At Iguazu Falls 32:03 
8. Four-Day Interval 37:42 
9. In Sarah, Mencken, Christ And Beethoven There Were Women And Men 42:29 
10. Almost Always Is Nearly Enough 50:01 
11. Jetty 52:45 
12. Everglade 1:01:09 1:05:32
  • I'd just like to point out that both answers on this question came in: (a) 10.5 minutes after it was asked, and more amazingly, (b) 15 seconds apart. (#TooSlow, @terdon! 😉) – FeRD Dec 9 '18 at 5:42
0

Using GNU awk or mawk:

$ awk -v RS='[0-9]+\\.' 'NR > 1 { print NR-1 ".", $0 }' file
1.  TNT 00:00
2.  Swing From The Gutters 7:34
3.  Ten-Day Interval 13:29
4.  I Set My Face To The Hillside 18:16
5.  The Equator 24:26
6.  A Simple Way To Go Faster Than Light That Does Not Work 28:27
7.  The Suspension Bridge At Iguazu Falls 32:03
8.  Four-Day Interval 37:42
9.  In Sarah, Mencken, Christ And Beethoven There Were Women And Men 42:29
10.  Almost Always Is Nearly Enough 50:01
11.  Jetty 52:45
12.  Everglade 1:01:09 1:05:32

The awk code reads the line as a set of records separated by a positive integer and a dot ([0-9]+\.). The body of the code simply prints the current record number (minus one, as the first record is the empty one before the initial 1.), a dot, and then the rest of the record.

0

A masochistic GNU grep solution

grep -oP '\d+[.]((?!\d+[.]).)+(?=\h)' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.