天天看點

sed及awk提取影片下載下傳位址

檔案内容為

[root@extmail changename]# cat test

第01集 http://down.hjzlg.com/bthy/prc001ts.torrent

第02集 http://down.hjzlg.com/bthy/prc003ts.torrent

需求如下:

去除行首的漢字部分,保留下載下傳位址部分。

方案一: 通過awk實作

[root@extmail changename]# cat test | awk '{print $2}'

http://down.hjzlg.com/bthy/prc001ts.torrent

http://down.hjzlg.com/bthy/prc003ts.torrent

<a href="http://down.hjzlg.com/bthy/prc004ts.torrent" target="_blank">http://down.hjzlg.com/bthy/prc004ts.torrent</a>

若想把空行也替換掉,則需加入sed管道,如下

[root@extmail changename]# cat test | awk '{print $2}' | sed /^$/d

其中awk '{print $2}'表示列印出每行中的第二個元素,sed /^$/d表示删除空行。

方案二:通過awk裡的substr函數

[root@extmail changename]# cat test | awk '{print substr($0,length($1)+2)}' | sed /^$/d

awk '{print substr($0,length($1)+2)}'表示傳回從第[length($1)+2]個字元開始的所有字元

sed /^$/d 表示删除空行

附注: substr(string,position,len) 傳回string的一個以position開始len個字元的子串

方案三:通過awk結合正則實作

[root@extmail changename]# 

cat test|awk '{print gensub(/.*(http.*torrent).*/,"\\1","g",$0)}' |sed /^$/d

http://down.hjzlg.com/bthy/prc004ts.torrent

awk '{print gensub(/.*(http.*torrent).*/,"\\1","g",$0)}'表示比對以http開頭torrent結尾的字元串,g代表替換全部,$0表示目前行。  執行後即把目前行以http開頭torrent結尾的字元串提取出來。

gensub函數以“,”作為分割符,共分為四部分。

本文轉自 xoyabc 51CTO部落格,原文連結:http://blog.51cto.com/xoyabc/1637242,如需轉載請自行聯系原作者