Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • Much cleaner that what I wrote. I started going down this path (of negation of slash within the match) but I didn't ever get it working cos I messed up the slash escaping. Using | as the sep is a very good idea in this context. Commented Jan 11, 2018 at 16:22
  • Great answer, the one liner does it cleanly and answer is well explained. Thanks Commented Jan 12, 2018 at 8:44
  • Yes, As @PesaThe has implied, you can also do sed -e 's|^directory_root \(/[^/]*/\).*$|\1|;tx;d;:x' file.txt, which does the same thing, but uses one less process (slightly more efficient). Outside of that, they produce the same result, which satisfies the adage: "There's more than one way to skin a cat"... (sorry, couldn't resist). Commented Jan 12, 2018 at 15:35
  • Also, note that you can start a new cycle with omitting the label name. So if you insist on using t command: sed '...$|\1|;t;d' will have the same effect.
    – PesaThe
    Commented Jan 16, 2018 at 23:25