Revisitando isso novamente e tentando usar nada além de um shell Bash, outra solução de uma linha é:
while read url; do url="${url##*/}" && echo "${url%%\'*}"; done < file.in > file.out
Onde file.in contém a lista de URLs 'sujos' e file.out conterá a lista de URLs 'limpos'. Não há dependências externas e não há necessidade de gerar novos processos ou subshells. A explicação original e um script mais flexível seguem. Há um bom resumo do método aqui , veja o exemplo 10-10. Isso é substituição de parâmetro baseada em padrão no Bash.
Expandindo a ideia:
src="define('URL', 'http://url.com');"
src="${src##*/}" # remove the longest string before and including /
echo "${src%%\'*}" # remove the longest string after and including '
Resultado:
url.com
Não há necessidade de chamar nenhum programa externo. Além disso, o seguinte script bash get_urls.sh, permite ler um arquivo diretamente ou a partir do 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
cat file.php | grep 'URL' | cut -d "'" -f 4.