by jlp__inf · 8mo ago · 0 comments
So if I understand correctly, it’s selection-based, right? You first select a region, and then you can apply further selections inside it?
So you kind of build nested selections to match exactly what you want (almost like a conditional tree).
And then you apply operations on the current selection
For example substitution with something like c/PATTERN/, is that correct?
. The dot (".") never matches newlines, which keeps line-oriented idioms from accidentally spanning newlines [1]
. Changes must be sequential and non-overlapping (that's why I deleted the whole thing before processing in the third example).
. Sam matches only the original input, not past changes.
. Addresses (expressions before commands) select a single range, x commands select multiple ranges and loop over it.
[1]: https://p9f.org/sys/doc/sam/sam.html, Regular expressions
Also, while i'm learning it, maybe that the sam traduction for the first file would be simpler, like:
,x/\n +/ c/ /
Which taught me a lot, because at first, i did not understand why we would stop at last line - 2 ($ - 2), but in fact yeah last line is the very last "\n", that is why we stop at $ - 2 to not do E->weird.
I'm starting to truly love sam
based on what you wrote:
i just add trailing \n normalization at the end
, x/(.+\n)+|\n+/ { g/./ x/\n/ c/ / v/./ c/\n/ } $ a/\n/ , x/\n+$/ c/\n/ w file_out.txt q
, x/(.+\n)+/ .,+#0-#1 x/\n/ c/ /
, x/\n+/ c/\n/
(I haven’t tested it.)I’m glad that sam worked out for you. You can learn more from [1] and [2]. If you need any help, you’re always welcome to ask me.
[1]: https://ratfactor.com/papers/sam_tut.pdf
[2]: https://9p.io/sources/contrib/steve/other-docs/struct-regex....