shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] Capturar padrão de uma expressão regular


From: jimmy
Subject: Re: [shell-script] Capturar padrão de uma expressão regular
Date: Wed, 20 May 2009 09:35:03 -0300
User-agent: Mutt/1.4.2.3i

On Tue, May 19, 2009 at 06:29:04PM -0300, Leonardo wrote:
> Olá,
> 
> Tenho um arquivo com o seguinte conteúdo:
> 
> <pre><img src="/icons/blank.gif" alt="Icon "> <a
> href="?C=N;O=D">Name</a>                    <a href="?C=M;O=A">Last
> modified</a>      <a href="?C=S;O=A">Size</a>  <a
> href="?C=D;O=A">Description</a><hr><img src="/icons/back.gif" alt="[DIR]">
> <a href="/downloads/patches/">Parent
> Directory</a>                             -
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="graph_search.patch">graph_search.patch</a>      02-Mar-2009 21:50
> 1.0K
> <img src="/icons/unknown.gif" alt="[   ]"> <a
> href="page_length_graph_view.patch">page_length_graph_vi..&gt;</a>
> 02-Mar-2009 21:50  1.4K
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="ping_timeout.patch">ping_timeout.patch</a>      02-Mar-2009 21:50
> 2.4K
> <img src="/icons/folder.gif" alt="[DIR]"> <a
> href="pre-patched/">pre-patched/</a>            02-Mar-2009 22:05    -
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="snmp_string_issue_with_rrdtool_creation.patch">snmp_string_issue_wi..&gt;</a>
> 02-Mar-2009 21:50  336
> <hr></pre>
> <address>Apache Server at www.cacti.net Port 80</address>
> </body></html>
> 
> e estou interessado em pegar o nome de todos os arquivos que tenham seu nome
> terminado com ".patch".
> 
> Assim, tentei fazer:
> 
> $ cat cacti.html | egrep "*\.patch"
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="graph_search.patch">graph_search.patch</a>      02-Mar-2009 21:50
> 1.0K
> <img src="/icons/unknown.gif" alt="[   ]"> <a
> href="page_length_graph_view.patch">page_length_graph_vi..&gt;</a>
> 02-Mar-2009 21:50  1.4K
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="ping_timeout.patch">ping_timeout.patch</a>      02-Mar-2009 21:50
> 2.4K
> <img src="/icons/text.gif" alt="[TXT]"> <a
> href="snmp_string_issue_with_rrdtool_creation.patch">snmp_string_issue_wi..&gt;</a>
> 02-Mar-2009 21:50  336
> 
> Beleza, só que agora eu não sei como faço para capturar apenas a parte usada
> como parâmetro do href, como, por exemplo, graph_search.patch.
> 
> Qual ferramenta ou comando eu poderia usar para ter como saída apenas a
> lista dos arquivos que são referenciados nesses href's? Tem jeito de fazer
> isso com (e)grep?
> 
> 
> Atc,
> 
> Leonardo Andrade.
> 
> ------------------------------------

assumindo que essas tags sempre estejam na mesma linha que o nome do
arquivo, você pode tentar com o sed:

  sed '/.*href="\([^"]\+\.patch\)".*/!d;s//\1/' arquivo

com o grep você iria ter de fazer algo parecido com isto:

  grep -o 'href="[^"]\+\.patch' shell.tmp | cut -d\" -f2

você pode ainda fazer um looping e usar o expr:

while IFS=$'\n' read l; do
  patch=$(expr "$l" : '.*href="\([^"]\+\.patch\)".*')
  if [[ -n $patch ]]; then
     echo $patch
  fi
done < arquivo

ou ainda só com o bash mesmo:

while IFS=$'\n' read l; do
  [[ "$l" =~ .*href=\"\([^\"]\+\.patch\)\".* ]] &&
  echo ${BASH_REMATCH[1]};
done < arquivo



-- 
"Não manejo bem as palavras
Mas manipulo bem as strings."
------------------------------


reply via email to

[Prev in Thread] Current Thread [Next in Thread]