Linux / UNIX: Sed Replace Newline (\n) character

Linux / UNIX: Sed Replace Newline (\n) character

To replace the newline (\n) character with another string using the sed command on a Linux or Unix system, you can use the s/pattern/replacement/ syntax and specify the \n character as the pattern.

Here is an example of how to use the sed command to replace the newline character with a comma:

sed 's/\n/,/g' /path/to/input-file > /path/to/output-file
Sourc‮www:e‬.lautturi.com

Replace /path/to/input-file with the path to the input file and /path/to/output-file with the path to the output file.

This will replace all occurrences of the newline character with a comma in the input file and write the result to the output file.

You can use the sed command to replace the newline character with any other string you want. For example, to replace the newline character with a space, you can use the following command:

sed 's/\n/ /g' /path/to/input-file > /path/to/output-file

Keep in mind that the sed command operates on a line-by-line basis, so it may not be suitable for replacing newline characters within a line of text. In such cases, you may need to use a different method to replace the newline character.

Created Time:2017-10-29 22:08:57  Author:lautturi