ed(1) conference is a user on bsd.network. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
ed(1) conference @ed1conf

Want to underline/mark the last instance of a pattern on each line using ed(1)?

g/pattern/t.\
s/\(.*\)\(pattern\).*/\1\
\2/\
s/./^/g\
-s/./ /g\
j

puts a sequence of "^"s under the last match on each line.

· Web · 2 · 5

Small issue if the only instance of /pattern/ falls at the beginning of the line. This should fix it:

g/pattern/t.\
s/\(.*\)\(pattern\).*/x\1\
\2/\
s/./^/g\
-s/./ /g\
s/^ //\
j

Otherwise the "s/./ /g" fails.

@ed1conf Can you give an example of pattern this addresses?

@akpoff Can use it to underline (or under-caret?) text. Want to see the last instance of "Mercutio" on each line as you review your text of Romeo & Juliet?
g/Mercutio/t.\
s/\(.*\)\(Mercutio\).*/x\1\
\2/\
s/./^/g\
-s/./ /g\
s/^ //\
j

@ed1conf Nevermind. I got it to fail using the first pattern, and work with the second.

Nice work!