Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have one file contains data as follows...


How can I get the output that prints till 3rd occurrence of "/" in each line?

share|improve this question

migrated from stackoverflow.com Dec 9 '12 at 16:22

This question came from our site for professional and enthusiast programmers.

Anything that you have tried ? –  GajananH Dec 7 '12 at 8:31
If your input is representative, it would probably be better to instead print everything up to the first ?, and awk '{print $1}' FS=? works nicely. –  William Pursell Dec 7 '12 at 16:38
add comment

3 Answers 3

up vote 1 down vote accepted
awk -F/ 'BEGIN{OFS="/";}{print $1,$2,$3}' your_file
share|improve this answer
Thank you sarathi. –  Lingaraj Dec 7 '12 at 10:02
add comment

It's as simple as this: cut -d'/' -f1,4 <file


$ echo /foo/bar/baz/extra | cut -d'/' -f1-4
share|improve this answer
add comment
sed 's_\(/.*/.*\)/.*_\1_' your-file.txt

This is an example:

>sed 's_\(/.*/.*\)/.*_\1_' your-file.txt
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.