views:

1579

answers:

4

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.

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

For characters up to ':':

echo 'Monday:8am-10pm' | sed -e 's/^[^:]*:/&\n/'
strager
jthompson
+5  A: 
sed 's/Monday:/&\n/g'
jthompson
Nice and simple, thanks jthompson!
Dennis
I see what you did there.
strager
+2  A: 
sed 's/Monday:/&\n\n/g'

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

John T
The 2 newlines was a mistake on my part. I meant for it to be just 1 newline. But you would be correct if that were the case. Thanks.
Dennis
In that case, you accepted the right answer :)
John T
A: 

Using sed:

echo "Monday:8am-10pm" | sed -e 's/:/:\n\n/'
vezult
Looks like you messed up your line a little bit.
strager
yeah, fixed now.
vezult