Skip to main content
12 events
when toggle format what by license comment
Nov 13, 2021 at 11:27 comment added virtualdj @Quasímodo Thanks for your answer! In my case, the q; is necessary because I don't get any output (no lines) without it. So the one-liner in the answer is correct.
Nov 12, 2021 at 15:29 comment added Kusalananda @Quasímodo A sed with potentially larger user base, on macOS (and therefore possibly on FreeBSD too, I haven't tested), fails.
Nov 12, 2021 at 15:16 comment added Quasímodo @schrodiger, I'm glad it was useful. I now realize the q command is not even needed, sed ':a;${s/^/# /;s/\n/&# /g;};N;4,$D;ba' file is enough because N works like q if called from the last line of the file.
Nov 12, 2021 at 15:15 comment added Quasímodo @they Indeed, the one-liner is not POSIX compliant, but it is just a matter of inserting newlines instead of semicolons to make it so. It is not true that it assumes GNU Sed only, it worked fine with OpenBSD too. For a one-liner POSIX command, one can use sed -e :a -e '$s/^/# /;$s/\n/&# /g;N;4,$D;b a' file.
Nov 12, 2021 at 7:49 comment added Kusalananda @virtualdj It would be highly unusual if you did not have tail installed. It's a standard utility and using it to solve this issue is the correct way of doing it. The sed "one-liner" is unreadable and therefore unmaintainable and not something you'd want to run in a production environment. The "one-liner" presented here also is non-portable, as it assumes GNU sed only.
Nov 11, 2021 at 21:13 comment added schrodingerscatcuriosity Upvoters! The sed solution is from @Quasímodo, they added it instead of posting their own answer. Many thanks to they.
Nov 11, 2021 at 19:08 vote accept virtualdj
Nov 11, 2021 at 18:31 history edited Quasímodo CC BY-SA 4.0
added 78 characters in body
Nov 11, 2021 at 17:53 comment added virtualdj For the same reasons of the original question (unix.stackexchange.com/q/107387/188792), i.e. I have a system with limited binaries available.
Nov 11, 2021 at 17:52 comment added schrodingerscatcuriosity @virtualdj ok, I'll try that, could you explain why sed only?
Nov 11, 2021 at 17:51 comment added virtualdj I would prefer to use sed only, as requested in the question.
Nov 11, 2021 at 17:48 history answered schrodingerscatcuriosity CC BY-SA 4.0