![unix 命令打印直到每行第三次出现“/”](https://linux22.com/image/43699/unix%20%E5%91%BD%E4%BB%A4%E6%89%93%E5%8D%B0%E7%9B%B4%E5%88%B0%E6%AF%8F%E8%A1%8C%E7%AC%AC%E4%B8%89%E6%AC%A1%E5%87%BA%E7%8E%B0%E2%80%9C%2F%E2%80%9D.png)
我有一个文件包含如下数据......
/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