How can I replace two newline characters \n\n
with a null \0
character in bash, and vice-versa?
I see that td
can replace a single newline with null, but not two consecutive newline characters. I tried sed
, but it doesn't seem possible. sed
seems "line based" and doesn't like messing with newline characters.
My reason for wanting to do this is to be able sort a file that has records separated by empty lines using sort -z
. For example, given a file
record b
foo bar
record a
zee bee
dee da
I want to pipe that file into a transform that replaces blank lines with null, then into sort -z
, and then replace null with blank lines, and finally have it spit out
record a
zee bee
dee da
record b
foo bar