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.
Then I realized I already knew Perl (and Perl one-liners), so there it sat unused on the shelf.
: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 the right tool, however limited (and sam is far from being universal), 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.)
supriyo-biswas•1h ago
adelks•1h ago
surgical_fire•1h ago
TheChaplain•41m ago
https://www.grymoire.com/Unix/Sed.html
supriyo-biswas•23m ago