1

I need to trim URLs after the third / . I removed the http from the beginning to make it easier.

Example:

a.ssl.fastly.net/static/r07/auth014.js
0.tqn.com/f/f/a/a.ttf?v=3.0.1

This should return

a.ssl.fastly.net/static/r07
0.tqn.com/f/f

I tried

echo "0.tqn.com/f/f/a/a.ttf?v=3.0.1" | sed 's/.*[\/].*[\/].*[\/].*/\1/'

but it does not seem to work and throws an error :

sed: -e expression #1, char 26: invalid reference \1 on `s' command's RHS

Can anyone pls help me?

4

4 回答 4

3

你也可以使用cut

cut -d/ -f1-3

例子:

$ echo "0.tqn.com/f/f/a/a.ttf?v=3.0.1" | cut -d/ -f1-3
0.tqn.com/f/f
于 2013-09-27T21:41:05.870 回答
2

您可以/用作分隔符并使用 awk 打印:

awk -F/ '{print $1"/"$2"/"$3}' file
于 2013-09-27T21:39:30.673 回答
1

这可能对您有用(GNU sed):

sed 's|/[^/]*||3g' file

使用 a|作为替换命令的分隔符,从第 3 次出现到行尾删除 a/后跟任意数量的非'。/

于 2013-09-28T13:47:21.117 回答
0

这将删除最后一个字段:

awk -F/ '{$NF=""}1' OFS=/
a.ssl.fastly.net/static/r07/
0.tqn.com/f/f/a/
于 2013-09-27T21:40:42.887 回答