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

2019-06-05 21:38发布

问题:

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/'