繁体   English   中英

sed或awk捕获部分网址

[英]sed or awk to capture part of url

我对正则表达式和sed / awk脚本不太熟悉。

我的网址类似于以下洪流网址:

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

我想用sedawk脚本在标题后提取文本,即从上面的示例中得到:

反对carly.epub.rope.ropes.torrent

使用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

只需删除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

比方说:

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

通过使用sed (在示例中的regexp中无需提及title ):

 sed 's/.*=//'

另一个解决方案是cut与另一个标准的unix工具:

 cut -d= -f2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM