文件内容为

[root@extmail changename]# cat test
第01集 http://down.hjzlg.com/bthy/prc001ts.torrent

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

第03集 http://down.hjzlg.com/bthy/prc004ts.torrent


需求如下:


去除行首的汉字部分,保留下载地址部分。


方案一: 通过awk实现


[root@extmail changename]# cat test | awk '{print $2}'
http://down.hjzlg.com/bthy/prc001ts.torrent

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

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


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

[root@extmail changename]# cat test | awk '{print $2}' | sed /^$/d
http://down.hjzlg.com/bthy/prc001ts.torrent
http://down.hjzlg.com/bthy/prc003ts.torrent
http://down.hjzlg.com/bthy/prc004ts.torrent


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


方案二:通过awk里的substr函数


[root@extmail changename]# cat test | awk '{print substr($0,length($1)+2)}' | sed /^$/d
http://down.hjzlg.com/bthy/prc001ts.torrent
http://down.hjzlg.com/bthy/prc003ts.torrent
http://down.hjzlg.com/bthy/prc004ts.torrent


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/prc001ts.torrent
http://down.hjzlg.com/bthy/prc003ts.torrent
http://down.hjzlg.com/bthy/prc004ts.torrent


awk '{print gensub(/.*(http.*torrent).*/,"\\1","g",$0)}'表示匹配以http开头torrent结尾的字符串,g代表替换全部,$0表示当前行。  执行后即把当前行以http开头torrent结尾的字符串提取出来。

gensub函数以“,”作为分割符,共分为四部分。