Command

unix 命令列印到每行第三次出現“/”

  • August 4, 2014

我有一個文件包含如下數據…

/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

如何獲得在每行中第三次出現“/”之前列印的輸出?

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

就這麼簡單——用’/‘分隔,剪切欄位1-4:

cut -d'/' -f1-4

例子:

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

引用自:https://unix.stackexchange.com/questions/58018