5
\$\begingroup\$

Example:

echo -ne 'a\n\nb\nc\n\n\n\nd\n\n\n' | your-command

should output the same as

echo -ne 'ab\ncd\n'

Watch out, some POSIX tools have implementations that diverge from the standard. Please consult https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html before submitting your solutions.

\$\endgroup\$
0
1
\$\begingroup\$

Sed, 22

sed ':l;s/\n\n//;N;bl'

Try it online!

Initially I had hoped to do sed -z 's/\n\n//g' for a score of 18, but I think -z is a GNU extension and not Posix.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry, it doesn't work on MacOS. -z option is not present either. (This version of sed was written by Diomidis Spinellis for FreeBSD. It's expected to be a superset of the IEEE Std 1003.2 (``POSIX.2'') specification.) \$\endgroup\$ Feb 6 '20 at 18:44
  • 1
    \$\begingroup\$ Figured it out: sed -E ':l;s/\n\n//;N;bl' on MacOS. A known bug. \$\endgroup\$ Feb 7 '20 at 12:42
1
\$\begingroup\$

AWK, 26 22 bytes

awk -vRS='

' -vORS= 1

Tested with gawk and mawk on MacOS.

Edit: saved 4 bytes thanks to @AndersKaseorg.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can remove the second "" pair (−2 bytes), and you can replace the \n\n with two literal newlines (−2 bytes). \$\endgroup\$ Feb 6 '20 at 9:21
1
\$\begingroup\$

Bash, 32 bytes

awk 'BEGIN{RS="\n\n";ORS="";} 1'

Try it online!

\$\endgroup\$
8
  • 1
    \$\begingroup\$ I'm not talking about practical use though. I'm saying your answer is invalid, since nothing in the challenge's spec says you can assume there's a character that's not in the input string. \$\endgroup\$
    – Grimmy
    Feb 6 '20 at 17:14
  • \$\begingroup\$ See this standard loophole. \$\endgroup\$
    – Grimmy
    Feb 6 '20 at 17:34
  • \$\begingroup\$ Your answer is invalid regardless of tags. Most standard loopholes (including this one) aren’t tag-specific. \$\endgroup\$
    – Grimmy
    Feb 7 '20 at 9:32
  • \$\begingroup\$ @Grimmy Ah, didn't know that - thanks for bring me up to speed. :-) \$\endgroup\$
    – Noodle9
    Feb 7 '20 at 11:07
  • 1
    \$\begingroup\$ @Grimmy Fixed that. \$\endgroup\$
    – Noodle9
    Feb 7 '20 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.