unix 命令打印直到每行第三次出现“/”

unix 命令打印直到每行第三次出现“/”

我有一个文件包含如下数据......

/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

如何获得打印到每行第三次出现“/”的输出?

答案1

就这么简单 - 用“/”分隔,剪切字段 1-4:

cut -d'/' -f1-4

例子:

$ echo /foo/bar/baz/extra | cut -d'/' -f1-4
/foo/bar/baz

答案2

awk -F/ 'BEGIN{OFS="/";}{print $1,$2,$3}' your_file

答案3

sed 's_\(/.*/.*\)/.*_\1_' your-file.txt

这是一个例子:

>sed 's_\(/.*/.*\)/.*_\1_' your-file.txt
/tutordashboard/manageTutorLogInStatus
/tutordashboard/manageTutorLogInStatus

答案4

perl

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

相关内容