The “sed” command line utility basically acts as a “stream editor” that manipulates the text files and streams. It is used to edit text files, search and replace strings/patterns/characters, add/delete lines from the file, select the text, and much more.
The main advantage of the “sed” command is it implements all editing instructions to a file by simply executing the single command using its supported flags. It also assists the users in searching and replacing the character in a file globally or at a specific position.
This post shows the possible ways to replace the newline character with the comma using the sed command:
- Using “-z” Option
- Using “H, h, d, x, and y” Options
- Using “-n, H, h, g, and p” Options
- Using “H, x, p” Options
- Using “a, b, $! and N” Options
Example 1: Using “-z” Option
The “sed” command offers the “-z” flag to replace the “\n(newline)” character with the “,(comma)” of the “itslinuxfoss.txt” file in this way:
$ sed -z 's/\n/,/g;s/,$/\n/' itslinuxfoss.txt
The above “sed” command contains the following flags:
- z: Converts the “\n(newline)” character with the null character (\0).
- s(substitute): Replaces the existing string/pattern with a new one.
- \n: separates each line of the file as a new line
- /g: Searches the specified pattern/character globally.
- $: Corresponds to the last line of the input file i.e “itslinuxfoss.txt”:
Output
It is shown that the “\n” character is replaced with the “,(comma)” globally in “itslinuxfoss.txt” file.
Example 2: Using “H, h, d, x, and y” Options
The combination of “H, h, d, x and y” can also be utilized for replacing the “\n” with the new character “,(comma)”. Follow the below-mentioned “sed” command to perform this task:
$ sed 'H;1h;$!d;x;y/\n/,/' itslinuxfoss.txt
The description of this “sed” command is illustrated here:
- H: Append the last line of the holding text.
- 1h: Copies each file name to the first line of holding text
- $!d: Deletes all new lines except the last line of the file.
- x: Exachnges the pattern space and the holding text.
- y: Replaces the “\n” character with the “,(comma)”.
Output
The above command has done its job successfully.
Example 3: Using “-n, H, h, g, and p” Options
The `sed` command uses the “-n” flag with another pattern of supported flags “H, h, g and p” for the replacement of a newline character to a comma. Let’s see how it works:
$ sed -n "H;1h;\${g;s/\n/,/g;p}" itslinuxfoss.txt
The command used the same flags(Example 2) but with the addition of two new flags i.e:
- -n: Prevents the automatic printing of the file content.
- P: Prints the input file content with the new changes that have occurred.
Output
The “\n” has been replaced with the new character “,(comma)” globally.
The first search and replace expression will replace each \n with a comma, and the second search and replace expression will replace a comma at the beginning of a line with a space.
Example 4: Using “H, x, p” Options
The “H, x, p” combination can also be used with the “sed” command for replacing the newline character with a comma in this way:
$ sed -n 'H;${x;s/\n/,/g;s/^,//;p;}' itslinuxfoss.txt
The following flags are also discussed in (Example 3):
Output
The desired output has been achieved.
Example 5: Using “a, b, $! and N” Options
The “a,b,$1, and N” is another combination of “sed” supported flags to change the “\n” occurrence with the “,(comma)”. The following command shows how this operation performs:
$ sed ':a;N;$!ba;s/\n/,/g' itslinuxfoss.txt
The “a,b,$!, and N” arguments description is as follows:
- a: Appends the tasks
- b: Branches the file content
- N: Moves to the next line
- $!: Prevents that the replacement is not applied on the last line
Output
The defined task of the “sed” command has been performed successfully.
Conclusion
The Linux “sed” command line tools use the supported flags “-z”, “H, h, d, x and y”, and “-n, H, h, g, and p ” to replace a “\n(newline)” to a “,(comma)”. This task can also be done easily with the “H, x, p” and “a, b, $! and N” combination of ”sed” arguments. All the flags performed their jobs in an organized manner.
This post has listed all possible methods to replace a new line with a comma using the sed command.