I have a file which likes to have consecutive empty lines (more clearly, whitespace-only lines). I want to substitute all of them with a single one.
I would prefer sed
(this task would be, in awk
, imho trivial).
I have a file which likes to have consecutive empty lines (more clearly, whitespace-only lines). I want to substitute all of them with a single one.
I would prefer sed
(this task would be, in awk
, imho trivial).
To squeeze consecutive empty lines (zero-characters lines) you could use a N;P;D cycle
:
sed '$!N;/^\n$/!P;D' infile
To squeeze consecutive blank lines just change the regex to account for possible blanks:
sed '$!N;/^[[:blank:]]*\n[[:blank:]]*$/!P;D' infile
If you have GNU sed
available (and it doesn't otherwise conflict with your script), you may use the -z
flag for zero-delimited lines and then have a simpler REGEX, e.g.:
sed -z 's/\n[\n ]*/\n/g' infile
Note that -z
is not POSIX.
sed
instances. – ilkkachu Jan 14 '24 at 19:29