1

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).

don_crissti
  • 82,805
peterh
  • 9,731
  • So, just out curiosity, why do you want to do it in sed if you already feel it'd be easier in AWK? :) (I'm not asking because I feel sed programs often feel like turing tarpits, no, nope, not me.) – ilkkachu Jan 14 '24 at 18:08
  • @ilkkachu Because it will be part of a larger sed script. I do not want to inject yet another interpreter only for this nuance. – peterh Jan 14 '24 at 19:26
  • ok, fair enough. Though note that sed has a number of functions that are not so welcoming to larger programs within the same sed invocation. Like all the "do something and implicitly jump to start" functions. Though perhaps you meant a pipeline of multiple sed instances. – ilkkachu Jan 14 '24 at 19:29
  • @ilkkachu The size of the script is about 7 lines, I can extend it to 8 line or insert awk into the system. I dislike multiple sed instances, but I choose them against a sed-awk pipeline. Actually, I had no problem also with a sed-awk pipe, I only have problem with unneeded complexity. I also think that 10 lines is not too big for a sed script, and I also think that sed can do a lot more than string replacements. – peterh Jan 14 '24 at 19:37
  • sure, as long as it works for you. I didn't mean to say there's anything bad in that, but yeah, it shows I just personally feel sed to be rather awkward. :) – ilkkachu Jan 15 '24 at 10:04

2 Answers2

1

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
don_crissti
  • 82,805
0

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.

FelixJN
  • 13,566