0

我对正则表达式和 sed/awk 脚本不是很有经验。

我有类似于以下 torrent url 的 url:

http://torcache.net/torrent/D7249CD9AF321C8578B3A7007ABBDD63B0475EEB.torrent?title=[kickass.to]against.the.ropes.by.carly.fall.epub.torrent

我想让sedawk脚本在标题之后提取文本,即从上面的示例中获取:

[kickass.to]反对.the.ropes.by.carly.fall.epub.torrent

4

4 回答 4

5

一个简单的方法awk:使用=作为字段分隔符:

awk -F"=" '{print $2}'

因此:

echo "http://torcache.net/torrent/D7249CD9AF321C8578B3A7007ABBDD63B0475EEB.torrent?title=[kickass.to]against.the.ropes.by.carly.fall.epub.torrent" | awk -F"=" '{print $2}'
[kickass.to]against.the.ropes.by.carly.fall.epub.torrent
于 2013-10-20T06:29:54.767 回答
3

只需删除 title= 之前的所有内容:sed 's/.*title=//'

$ echo "http://torcache.net/torrent/D7249CD9AF321C8578B3A7007ABBDD63B0475EEB.torrent?title=[kickass.to]against.the.ropes.by.carly.fall.epub.torrent" | sed 's/.*title=//'
[kickass.to]against.the.ropes.by.carly.fall.epub.torrent
于 2013-10-20T03:14:35.980 回答
3

比方说:

s='http://torcache.net/torrent/D7249CD9AF321C8578B3A7007ABBDD63B0475EEB.torrent?title=[kickass.to]against.the.ropes.by.carly.fall.epub.torrent'

纯 BASH 解决方案:

echo "${s/*title=}"
[kickass.to]against.the.ropes.by.carly.fall.epub.torrent

或使用grep -P

echo "$s"|grep -oP 'title=\K.*'
[kickass.to]against.the.ropes.by.carly.fall.epub.torrent
于 2013-10-20T07:08:15.133 回答
1

通过使用sed(无需title在您的示例中的正则表达式中提及):

 sed 's/.*=//'

另一个解决方案存在于cut另一个标准 unix 工具中:

 cut -d= -f2
于 2013-10-20T08:54:39.650 回答