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

不想你离开。 提交于 2020-01-03 02:25:10

问题


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.


回答1:


sed 's/Monday:/&\n/g'



回答2:


echo 'Monday:8am-10pm' | sed -e 's/^Monday:/&\n/'

For characters up to ':':

echo 'Monday:8am-10pm' | sed -e 's/^[^:]*:/&\n/'



回答3:


sed 's/Monday:/&\n\n/g'

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




回答4:


Using sed:

echo "Monday:8am-10pm" | sed -e 's/:/:\n\n/'


来源:https://stackoverflow.com/questions/682984/shell-adding-a-new-line-between-a-given-line-of-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!