Search code examples
regexlinuxbashsedcut

Remove everything after a changing string


I'm having some trouble with the following problem;

As input, I get a few lines of paths to files as follows:

root/child/abc/somefile.txt
root/child/def/123/somefile.txt
root/child/ghijklm/somefile.txt

The root/child piece is always in the path, everything after can differ.

I would like to remove everything after the grandchild folder. So the output would be:

root/child/abc/
root/child/def/
root/child/ghijklm/

I've tried the following:

sed 's/\/child\/.*/\/child\/.*/'

But of course that would just give the following output:

root/child/.*
root/child/.*
root/child/.*

Any help would be appreciated!


Solution

  • with cut:

    cut -d\/ -f1,2,3 file