2014-07-16 2 views
0

에서 URL을 포함하는 열 추출 :이 같은 파일이 텍스트 파일

Timestamp  URL     Text      
1331635241000 http://example.com  Peoples footage at www.test.com,http://example4.com 
1331635231000 http://example1.net crack the nuts http://example6.com 
1331635280000 http://example2.net Loving this 

각 열은 탭으로 분리됩니다. 나는 예를 들어이 같은 결과를 얻을 수 있도록 다음 한 URL의 경우 2 열 및 3 열 경우 만 URL을 추출 빈을 떠날 필요 :이 스크립트를 시도

URL     Text 
http://example.com  www.test.com,http://example4.com 
http://example1.net http://example6.com 
http://example2.net  

awk 'BEGIN {FS="\t"} {print $2,$3}' file | grep -oP '(((http|https|ftp|gopher)|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)[^ .,;\t>">\):]' 

이 스크립트는 헤더가없는 단일 열에 모든 URL을 제공 할 수 있습니다. 이 문제를 해결하기위한 제안.

답변

0

그냥 하나 awk 스크립트에서 모든 작업을 수행합니다

awk ' 
BEGIN{ FS=OFS="\t" } 
NR==1 { print $2, $3; next } 
{ 
    urls = "" 
    while (match($3,/((https?|ftp|gopher|mailto)[.:][^ >"\t]*|www\.[-a-z0-9.]+)/)) { 
     urls = (urls ? urls "," : "") substr($3,RSTART,RLENGTH) 
     $3 = substr($3,RSTART+RLENGTH) 
    } 
    print $2, urls 
} 
' file 
URL  Text 
http://example.com  www.test.com,http://example4.com 
http://example1.net  http://example6.com 
http://example2.net 

내가 URL을 일치하는 재 확신을 완전히 정확 아니에요, 당신은 다시보고 할 수 있습니다.

관련 문제