Shell: adding a new line between a given line of t

2019-06-05 21:35发布

What this question isn't asking is how to add a new line below or above every line which matches a pattern.

What I'm trying to do is add a new line between a pattern that exists on one line.

Here is an example.

before:

Monday:8am-10pm

after:

Monday:

8am-10pm

Thus in this case, insert new line after every 'Monday' pattern.

4条回答
你好瞎i
2楼-- · 2019-06-05 22:11

Using sed:

echo "Monday:8am-10pm" | sed -e 's/:/:\n\n/'
查看更多
劳资没心,怎么记你
3楼-- · 2019-06-05 22:15
sed 's/Monday:/&\n/g'
查看更多
太酷不给撩
4楼-- · 2019-06-05 22:20
sed 's/Monday:/&\n\n/g'

will replace them (supposing you want 2 newlines as shown above)

查看更多
混吃等死
5楼-- · 2019-06-05 22:33
echo 'Monday:8am-10pm' | sed -e 's/^Monday:/&\n/'

For characters up to ':':

echo 'Monday:8am-10pm' | sed -e 's/^[^:]*:/&\n/'
查看更多
登录 后发表回答