Have not yet seen this answer, so here's how you can do this with vi
or vim
:
vi -c '%s/\(http:\/\/.\{-}\/\).*/\1/ge | wq' file &>/dev/null
This runs the vi
:%s
substitution globally (the trailing g
), refrains from raising an error if the pattern is not found (e
), then saves the resulting changes to disk and quits. The &>/dev/null
prevents the GUI from briefly flashing on screen, which can be annoying.
I like using vi
sometimes for super complicated regexes, because (1) perl is dead dying, (2) vim has a very advanced regex engine, and (3) I'm already intimately familiar with vi
regexes in my day-to-day usage editing documents.