sed/awk를 사용하여 URL에서 호스트 이름 제거

sed/awk를 사용하여 URL에서 호스트 이름 제거

다양한 도메인에 대한 URL 목록이 제공되었으며 sed, awk 또는 이와 유사한 것을 사용하여 호스트 이름을 제거하고 경로만 유지하고 싶습니다. 포트 또는 사용자 이름@비밀번호가 포함된 URL이 없습니다.

입력하다:

http://www.example.com/
https://www.example.com/
http://example.com/blog/
https://example.com/blog/
https://www.example.co.uk/blog/
https://example.co.uk/blog/
https://sub.example.co.uk/blog/
https://www.example.com/blog/
https://www.example.com/cases/page/4/
https://www.example.com/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
https://www.example.com/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
https://www.example.co.uk/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
https://sub.example.co.uk/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js

출력은 다음과 같아야 합니다.

/
/
/blog/
/blog/
/blog/
/blog/
/blog/
/blog/
/cases/page/4/
/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js

정규식 명령만 찾을 수 있으므로 누군가 나를 도울 수 있기를 바랍니다. sed 또는 awk 명령으로 올바르게 변환하는 방법을 모르겠습니다.

답변1

그리고 perl:

perl -pe 's|^([^/:]+:)?//[^/]*||' < your-file

대체 문자가 제거되고( http://host/path및 를 모두 처리하기 위해 //host/path) //해당 문자를 제외한 모든 문자가 제거됩니다(예 /: 및 가 제거됩니다 host).user:password@host:8080ftp://user:password@host:8080/pub

이에 상응하는 내용은 sed다음과 같습니다.

LC_ALL=C sed 's|^\([^/:]\{1,\}:\)\{0,1\}//[^/]*||' < your-file

어쨌든 s/pattern/replacement/and 연산자는 sed정규식 perl을 패턴으로 사용합니다.기본 정규식을 위한 sed,펄 정규식perl(이것은 개선되고 확장됩니다 .확장 정규식오늘날 많은 구현 sed에서도 이 옵션을 지원합니다.-E

URI를 구조화된 객체로 구문 분석하는 URI모듈 도 있습니다 .perl

perl -MURI -lpe '$_ = URI->new($_)->path' < your-file

쿼리 문자열( 에서와 같이 http://host/path?query)과 조각( 에서와 같이) http://host/file.html#anchor이 있는 경우 이를 삭제합니다. 쿼리를 포함하려면(있는 경우) ->path로 바꾸세요.->path_query

답변2

이는 Linux coreutils를 사용하여 쉽게 수행할 수 있습니다.

cut -d '/' -f 3- somefilewithyoururls.txt | sed 's/^/\//'

세 번째 이후의 모든 내용을 잘라 /내고 줄의 시작 부분을 /. 복잡한 정규 표현식이 필요하지 않습니다.

답변3

아무 sed나 사용하세요:

$ sed 's:[^/]*//[^/]*::' file
/
/
/blog/
/blog/
/blog/
/blog/
/blog/
/blog/
/cases/page/4/
/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js

답변4

사용행복하다(이전 Perl_6)

~$ raku -MURL -ne 'my $url = URL.new($_); put "/" ~ .path.join("/") for $url;'  file

예제 출력:

/
/
/blog
/blog
/blog
/blog
/blog
/blog
/cases/page/4
/cdn-cgi/challenge-platform/h/g/cv/result/7c9123dc38da6841
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js
/cdn-cgi/challenge-platform/h/g/scripts/jsd/7fe83wdcs/invisible.js

Raku의 경우 URL모듈을 로드하는 것이 URL에서 사용자 이름/비밀번호를 처리할 수 있으므로 아마도 가장 깔끔한 대답일 것입니다. 위에서 식별된 path요소 앞에는 슬래시가, join그 뒤에는 /슬래시가, 그 다음에는 out 이 옵니다 put.

위의 코드를 단순화하면 어떤 요소가 인식되는지 알 수 있습니다.

~$ raku -MURL -ne 'my $url = URL.new($_); .raku.put for $url;'  file
URL.new(scheme => "http", username => Str, password => Str, hostname => "www.example.com", port => Int, path => [], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.com", port => Int, path => [], query => {}, fragment => Str)
URL.new(scheme => "http", username => Str, password => Str, hostname => "example.com", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "example.com", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.co.uk", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "example.co.uk", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "sub.example.co.uk", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.com", port => Int, path => ["blog"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.com", port => Int, path => ["cases", "page", "4"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.com", port => Int, path => ["cdn-cgi", "challenge-platform", "h", "g", "cv", "result", "7c9123dc38da6841"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.com", port => Int, path => ["cdn-cgi", "challenge-platform", "h", "g", "scripts", "jsd", "7fe83wdcs", "invisible.js"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "www.example.co.uk", port => Int, path => ["cdn-cgi", "challenge-platform", "h", "g", "scripts", "jsd", "7fe83wdcs", "invisible.js"], query => {}, fragment => Str)
URL.new(scheme => "https", username => Str, password => Str, hostname => "sub.example.co.uk", port => Int, path => ["cdn-cgi", "challenge-platform", "h", "g", "scripts", "jsd", "7fe83wdcs", "invisible.js"], query => {}, fragment => Str)

정규식을 사용하여 URL을 구문 분석할 용기가 있다면(악의적으로 조작된 데이터가 없다고 확신하십니까?) @Stéphane_Chazelas가 게시한 Perl 답변을 상당히 직접적으로 번역한 내용은 다음과 같습니다.

~$ raku -pe 's|^ ( <-[/:]>+ \: )? \/ \/ <-[/]>* ||;'  < file

https://raku.land/cpan:TYIL/URL
https://raku.org

관련 정보