Sed, a powerfull mini-language from the 70s (julienlargetpiet.tech)

by random__duck 31 comments 19 points
Read article View on HN

31 comments

[−] supriyo-biswas 54d ago
This would have been a good article if the author actually took the time to write out the article themselves, perhaps in their native language, rather than using a LLM to write it.
[−] TheChaplain 54d ago
This one feels more authentic.

https://www.grymoire.com/Unix/Sed.html

[−] supriyo-biswas 54d ago
I actually prefer the "organization" of the original article, but could not continue past the LLMisms.
[−] jlp__inf 53d ago
Hi, I'm the author of the article, and sorry if it felt overly LLM-generated.

To clarify: I did spend quite a bit of time diving into sed myself (experiments, tests, etc.) and wrote an initial draft in French.

I then used ChatGPT to help me structure and refine the article in English to reach more people. But the core ideas and understanding come from my own exploration.

That said, I understand the concern, and for future articles I'll try to rely less on that kind of tooling for structure.

Happy to clarify or go deeper on any part of the article if needed.

[−] surgical_fire 54d ago
Man, I don't even hate LLM for the sake of it, but this LLM language and formatting is really grating after a while.
[−] adelks 54d ago
Indeed.
[−] evanjrowley 54d ago
For a while my home had a Raspberry Pi 2B running FreeBSD 10 acting as a router-on-a-stick. When I lost SSH connectivity to it, I would instead use GNU screen as with a serial cable. For whatever reason, I could never get "full screen" TUI apps like vi and nano to display properly. To edit files (like pf.conf for firewall rules), I had to use sed to edit lines of the file. It was an interesting learning experience and perhaps a glimpse of how things used to be with green screen terminals. Shortly thereafter I switched over to a Beaglebone Green with OpenBSD 5 and never needed that workaround again.
[−] phplovesong 54d ago
For those of us using vim/neovim sed is something everyone had to learn, even if they did not realize it.

This is the true power of vim. Even now decades later the unix toolbelt holds up, and is still unmatched for productivity.

Vim is in the end just a small piece of the puzzle. You use small tools for your problem, mix and match.

Its kind of like functional programming. Compose and reduce.

[−] jasonpeacock 54d ago
Long ago, I bought the O'Reilly "Sed & Awk" book with plans to become a true unix guru.

Then I realized I already knew Perl (and Perl one-liners), so there it sat unused on the shelf.

[−] aperrien 54d ago
Thanks for the blast from the past. SED led me to AWK, which led me to Perl, which lead me to Python. An interesting chain that brought me back to the interpreted languages like BASIC that I programmed in when I was a kid. Even though my formal training in college was Pascal and C.
[−] 0xfaded 54d ago
I learned sed back in the day to show off. I wish I'd invested that effort in learning perl oneliners instead. For whatever reason I picked up enough awk along the way, and now that's what I tend to use if I ever need something beyond a simple substitution.
[−] piekvorst 54d ago
I prefer sam [1]. Unlike sed, it's not Turing complete, but it is far more elegant to my taste - consider this example from the article:

    :loop N; s/\n[[:space:]]\+/ /g; t loop; p
In sam, the equivalent is:

    x/(.+\n)*/ x/\n */ c/ /
It reads like this: loop over paragraphs of non-empty lines, loop over newline followed by spaces, replace with a single space. It's surprisingly close to SQL in its eloquence.

Another example:

    N; h; s/\n/->/g ;p; g; D
In sam, an equivalent would be

    {
        1,$-2 x/./ {
            a/->/
            /./ t .
        }
        $-1 d
    }
Again, it's readable from top to bottom: from the first line to the second from the end, loop over each symbol, put "->" after it and copy the next symbol next to it; delete the last line.

Let's see how far we can get. Another example:

    N; h; s/\n/->/g; p; G; D
In sam, an equivalent would be:

    {
        d
        1,$-2 x/./ {
            1,/./ x/.|\n/ {
                g/./ t $
                g/\n/ $ c/->/
            }
            $ c/\n/
        }
    }
It reads like this: delete the whole thing; from the first line to the second from the end, loop over each character; on each iteration, from the first line to the next character, run an inner loop over each character or newline; if it's a character, put it at the end; otherwise, put "->" at the end; once the inner loop is done, put a newline at the end.

The final example from the post is too long to have it here (15 lines). Here's just the sam equivalent for it:

    x/(.+\n)+|\n+/ {
        g/./ x/\n/ c/ /
        v/./ c/\n/
    }
It reads like this: loop over paragraphs of non-empty lines or sequences of newline characters; if it has any symbol (that is, it's a paragraph), replace each newline symbol with a space; if it doesn't have a symbol (that is, it's a sequence of newline symbols), replace it with a single newline.

What I have learned from this is that a tool with limited but well-chosen primitives is more convenient than a universal state machine.

(My examples may not be the exact same algorithms, since I do not understand (or need) sed concepts, but they do produce the same output.)

[1]: https://9p.io/sys/doc/sam/sam.html