Bunu tekrar gözden geçirmek ve Bash kabuğundan başka bir şey kullanmaya çalışmak, başka bir tek çözüm:
while read url; do url="${url##*/}" && echo "${url%%\'*}"; done < file.in > file.out
Burada file.in 'kirli' url listesini ve file.out 'temiz' URL listesini içerecektir. Dış bağımlılıklar yoktur ve yeni süreçler veya alt kabuklar üretmeye gerek yoktur. Orijinal açıklama ve daha esnek bir komut dosyası gelir. Yöntemin iyi özeti Orada burada , örnek 10-10 görüyoruz. Bu, Bash'deki örüntü tabanlı parametre ikamesidir.
Fikir üzerine genişleyen:
src="define('URL', 'http://url.com');"
src="${src##*/}" # remove the longest string before and including /
echo "${src%%\'*}" # remove the longest string after and including '
Sonuç:
url.com
Harici programları çağırmanıza gerek yok. Ayrıca, aşağıdaki bash betiği, get_urls.shbir dosyayı doğrudan veya stdin'den okumanıza izin verir:
#!/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
cat file.php | grep 'URL' | cut -d "'" -f 4.