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...

/tutordashboard/manageTutorLogInStatus/?ua=TQ-AUTOCLOSE-REENTER&filterId=089089087087080&dt=1325757272
/tutordashboard/manageTutorLogInStatus/?ua=TQ-AUTOCLOSE-REENTER&filterId=089089087087080&dt=1873982869
/tutordashboard/manageTutorLogInStatus/?ua=TQ-BUTTON-DISABLED&sessionType=1&sessionId=4938718&filterId=97462&dt=1354822214604
/tutordashboard/manageTutorLogInStatus/?ua=TQ-BUTTON-DISABLED&sessionType=1&sessionId=4951063&filterId=95997&dt=1354807593071
/tutordashboard/manageTutorLogInStatus/?ua=TQ-BUTTON-DISABLED&sessionType=2&sessionId=4957338&filterId=99353&dt=1354752909284

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.

4  
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

5 Answers 5

up vote 2 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

It's as simple as this - separated by '/', cut fields 1-4:

cut -d'/' -f1-4

Example:

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

This is an example:

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

cut's better, but with sed:

sed 's|/|\n|3;P;d'

You might need a literal newline with some seds, though.

share|improve this answer

With perl:

$ perl -F'/' -anle 'BEGIN{$,="/"} print @{[@F[0..2]]}' file
/tutordashboard/manageTutorLogInStatus
/tutordashboard/manageTutorLogInStatus
/tutordashboard/manageTutorLogInStatus
/tutordashboard/manageTutorLogInStatus
/tutordashboard/manageTutorLogInStatus
share|improve this answer

Your Answer

 
discard

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.