如何替换一个字符并在其下一行的前面添加另一个字符?

问题描述 投票:0回答:1

我的目标是在特定行的末尾用

/
覆盖
-
字符,然后在该行的前面以及该行下一行的前面插入一个
-
。我所做的测试是使用 sed 如下:

$ cat input | sed -E 'N;s/^(- )?(.*\n)\//- \2-/;P;D'
$ cat input | sed 'N; \#\n/# { s//\n-/; s/^/- / }; P;D'
$ cat input | sed -E 'N;s|([^\n]*\n)/|- \1-|;P;D'
$ cat input | sed -Ez 's|([^\n]*\n)/|- \1-|g'

我的输入:

1
00:00:00,000 --> 00:00:00,000
<b><font color="#ffffff">Lorem Ipsum</font></b>

2
00:00:00,000 --> 00:00:00,000
Lorem ipsum dolor sit/
amet, consectetur adipiscing elit.

3
00:00:00,000 --> 00:00:00,000
Ut sed gravida nisi.

4
00:00:00,000 --> 00:00:00,000
Nunc sit amet ultrices urna,/
non efficitur neque. 

5
00:00:00,000 --> 00:00:00,000
<b><font color="#ffffff">Cras aliquam tristique dui, viverra laoreet diam hendrerit ac.</font></b>

我的期望:

00:00:00,000 --> 00:00:00,000
<b><font color="#ffffff">Lorem Ipsum</font></b>

2
00:00:00,000 --> 00:00:00,000
- Lorem ipsum dolor sit
- amet, consectetur adipiscing elit.

3
00:00:00,000 --> 00:00:00,000
Ut sed gravida nisi.

4
00:00:00,000 --> 00:00:00,000
- Nunc sit amet ultrices urna,
- non efficitur neque. 

5
00:00:00,000 --> 00:00:00,000
<b><font color="#ffffff">Cras aliquam tristique dui, viverra laoreet diam hendrerit ac.</font></b>
bash awk sed
1个回答
0
投票

awk

awk 'flag  {flag = 0; print "- " $0}
     /\/$/ {flag = 1; print "- " substr($0, 1, length($0) - 1)} input
© www.soinside.com 2019 - 2024. All rights reserved.