Search code examples
linuxbashawkgrepcut

Want to cut right before second trailing slash?


I wanted to cut the following rows right before second trailing slash.

Like

/home/john
/home/mathew
/home/alexander/public_html/path/to/file/1
/home/testuser/public_html/path/to/file/1
/home/hellouser/public_html/path/to/file/13

TO

/home/john
/home/mathew
/home/alexander
/home/testuser
/home/hellouser

How this can be done? using grep or cut or awk or sed? I am not sure.


Solution

  • You can use awk

    awk -F/ '{print "/" $2 "/" $3}'