sed Insert a Newline Into the RHS of a Substitution

www.l‮.iruttua‬com
sed Insert a Newline Into the RHS of a Substitution

To insert a newline into the right-hand side (RHS) of a substitution in sed, you can use the \n escape sequence. For example, the following command will insert a newline after the first occurrence of the string string1:

sed 's/string1/string1\n/' file

This will search the file for the first occurrence of string1, and replace it with string1 followed by a newline.

You can also insert a newline at the beginning of a substitution by using the ^ character:

sed 's/string1/\nstring1/' file

This will search the file for the first occurrence of string1, and replace it with a newline followed by string1.

If you want to insert a newline after every occurrence of a string, you can use the g flag at the end of the substitution command:

sed 's/string1/string1\n/g' file

This will search the file for all occurrences of string1, and replace them with string1 followed by a newline.

By using the \n escape sequence and the g flag in a sed substitution, you can insert newlines into the right-hand side of a substitution. This can be useful for formatting text data or for adding line breaks in a script.

Created Time:2017-10-30 14:27:35  Author:lautturi