There's a moment when using sed stops feeling like typing weird incantations… and starts feeling like you're programming a living stream of text. At first, it looks like this: And you think: "ok… print lines 1 and 2… neat, but whatever" But if you stay with it — if you push just a bit further — you discover something unexpected: sed is not a command. It is a language. A small one. A strange one. But a real one --> with control flow, memory, and a model of execution. And that's how you end up writing things like: script.sed Copy /./H /^$/ { x /./ { s/\n/ /g s/^ // p } } $ { x s/\n/ /g s/^ // /./ { p } } Copy sed -n -E -f script.sed ex3.log …and suddenly, you're not filtering text anymore. 👉 You're processing a stream. The first principle: sed is a stream machine At its core, sed does this: Copy read line → transform → (maybe print) → repeat One line at a time. No context. No memory. That's why the simplest commands feel trivial: Basic streaming Copy sed '1,2p' file # print lines 1 and 2 sed '/error/p' file # print lines matching "error" sed '5q' file # stop at line 5 sed '/foo/d' file # delete lines matching "foo" Core commands you must know Command Meaning p print d delete and skip rest q quit immediately s/// substitute Example 👉 disable default output (-n) and print explicitly Editing the stream You can also modify lines: Copy sed 's/foo/bar/g' file # replace text Insert / append / change These are surprisingly expressive: Copy sed '/Alice/i BEFORE' file # insert before sed '/Alice/a AFTER' file # append after sed '/Alice/c REPLACED' file # replace line Command Effect i insert before a append after c replace line You can combine streaming portions of the lines to processing them like doing: Copy sed -n '2,4 {/Alice/c REPLACED}; p' file Note that we seprarate the commands with ; and wrapped the change c command inside {}, so after we can print what happened. Some commands like c stops direclty further commands that is why we wrapped them. In fact c will stop furthe...
First seen: 2026-03-23 16:08
Last seen: 2026-03-23 16:08