我有以下示例:
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
我想将其转换为:
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
我遵循的逻辑路径是,我知道总会有一个“点”(.) 与曲目编号一起出现,我可以使用它作为休息的指导方针。问题是,它必须爬回一两个字符才能保留轨道数。
答案1
至少使用 GNU sed,您可以使用修饰符在除第一个匹配序列之外的所有序列之前插入换行符2g
:
sed -E 's/[0-9]+\./\n&/2g' file
答案2
只需捕获数字和点并在它们之前插入换行符:
$ 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
答案3
使用 GNUawk
或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
该awk
代码将该行读取为一组由正整数和点 ( [0-9]+\.
) 分隔的记录。代码主体只是打印当前记录号(减一,因为第一个记录是初始记录之前的空记录1.
)、一个点,然后是记录的其余部分。
答案4
一个受虐的 GNU grep 解决方案
grep -oP '\d+[.]((?!\d+[.]).)+(?=\h)' file