簡體   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