답변:
이 같은?
grep 'URL' file.php | rev | cut -d "'" -f 2 | rev
또는
grep 'URL' file.php | cut -d "'" -f 4 | sed s/'http:\/\/'/''/g
http : //를 제거합니다.
http://url.com하지 않았다url.com
/sed 와 일치 시키려면 일반적으로 다른 구분 기호를 사용해야합니다 (예 :) sed s@http://@@g.
간단한 방법으로 모든 것을 할 수 있습니다 grep.
grep -oP "http://\K[^']+" file.php
보낸 사람 man grep:
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression (PCRE, see
below). This is highly experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
트릭은 \K펄 정규식에서 의미하는 을 사용 하는 것 discard everything matched to the left of the \K입니다. 따라서 정규 표현식은 http://(로 인해 버림 \K)로 시작하고 가능한 많은 '문자가 아닌 문자열을 찾습니다 . 와 결합 -o하면 URL 만 인쇄됩니다.
펄에서 직접 할 수도 있습니다 :
perl -ne "print if s/.*http:\/\/(.+)\'.*/\$1/" file.php\
이것을 다시 방문하고 Bash 쉘 만 사용하려고하면 다른 한 줄 솔루션이 있습니다.
while read url; do url="${url##*/}" && echo "${url%%\'*}"; done < file.in > file.out
여기서 file.in에는 'dirty'URL 목록이 포함되고 file.out에는 'clean'URL 목록이 포함됩니다. 외부 의존성이 없으며 새로운 프로세스 나 서브 쉘을 생성 할 필요가 없습니다. 원래 설명과보다 유연한 스크립트가 이어집니다. 이 방법의 좋은 요약이 있습니다 여기에 , 예 10-10를 참조하십시오. 이것은 Bash에서 패턴 기반 매개 변수 대체입니다.
아이디어 확장 :
src="define('URL', 'http://url.com');"
src="${src##*/}" # remove the longest string before and including /
echo "${src%%\'*}" # remove the longest string after and including '
결과:
url.com
외부 프로그램을 호출 할 필요가 없습니다. 또한 다음 bash 스크립트를 사용 get_urls.sh하면 직접 또는 stdin에서 파일을 읽을 수 있습니다.
#!/usr/bin/env bash
# usage:
# ./get_urls.sh 'file.in'
# grep 'URL' 'file.in' | ./get_urls.sh
# assumptions:
# there is not more than one url per line of text.
# the url of interest is a simple one.
# begin get_urls.sh
# get_url 'string'
function get_url(){
local src="$1"
src="${src##*/}" # remove the longest string before and including /
echo "${src%%\'*}" # remove the longest string after and including '
}
# read each line.
while read line
do
echo "$(get_url "$line")"
done < "${1:-/proc/${$}/fd/0}"
# end get_urls.sh
[t]csh하므로 sh, bash, dash, ksh, zsh에
단순한:
php -r 'include("file.php"); echo URL;'
'http : //'를 제거해야하는 경우 :
php -r 'include("file.php"); echo URL;' | sed 's!^http://\(.*\)!\1!'
그래서:
myURL=$(php -r 'include("file.php"); echo URL;' | sed 's!^http://\(.*\)!\1!')
당신이 특정 필요한 경우 일부 당신이 당신의 용어를 수정하는 데 필요한 URL의를하는 URL은 모두 다음의, 때로는 더 :
URL := protocol://FQDN[/path][?arguments]
FQDN := [hostname.]domain.tld
cat file.php | grep 'URL' | cut -d "'" -f 4.