Grep - Procurando por conteúdo em arquivos
Dica publicada em Shell Script / Miscelânea
Grep - Procurando por conteúdo em arquivos
Olá pessoal,
Uma dica realmente muito simples, mas que pode nos ajudar bastante é o comando grep.
Muita gente até usa o grep para filtrar resultados de saída, como no exemplo abaixo:
$ ps aux | grep http
Supondo que procuramos agora por arquivos que contenham a string "http":
$ cd /etc/rc.d
$ grep http *
rc.0:if [ -x /etc/rc.d/rc.httpd ]; then
rc.0: /etc/rc.d/rc.httpd stop
rc.6:if [ -x /etc/rc.d/rc.httpd ]; then
rc.6: /etc/rc.d/rc.httpd stop
rc.K:if [ -x /etc/rc.d/rc.httpd ]; then
rc.K: /etc/rc.d/rc.httpd stop
rc.M:if [ -x /etc/rc.d/rc.httpd ]; then
rc.M: . /etc/rc.d/rc.httpd start
rc.inet2:# See the HOWTOs on http://www.netfilter.org/ for documentation on
rc.modules:# USB scanner support (requires patched SANE from http://fachschaft.cup.uni-muenchen.de/~neukum/scanner.html):
rc.modules:# for telnet and http, for instance. But, the system breaks down when you
rc.wireless.conf:# http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html
Além disso, o comando poderia conter a flag -R:
$ grep http * -R
(Para procurar recursivamente nos diretórios)
Espero que essa dica seja útil!
[]'s
Ragen
Uma dica realmente muito simples, mas que pode nos ajudar bastante é o comando grep.
Muita gente até usa o grep para filtrar resultados de saída, como no exemplo abaixo:
$ ps aux | grep http
root 1716 0.0 0.8 105656 2188 ? Ss Sep01 0:00 /usr/sbin/httpd -DSSL nobody 1747 0.0 6.3 111256 16276 ? S Sep01 2:37 /usr/sbin/httpd -DSSL nobody 1748 0.0 7.4 110792 19024 ? S Sep01 2:31 /usr/sbin/httpd -DSSL nobody 1749 0.0 7.4 110424 19040 ? S Sep01 2:04 /usr/sbin/httpd -DSSL nobody 1928 0.0 5.5 110448 14136 ? S Sep01 2:14 /usr/sbin/httpd -DSSL nobody 1962 0.0 3.9 111480 9984 ? S Sep01 2:13 /usr/sbin/httpd -DSSL nobody 1971 0.0 3.7 110432 9544 ? S Sep01 2:53 /usr/sbin/httpd -DSSL nobody 1972 0.0 4.7 111512 12100 ? S Sep01 2:36 /usr/sbin/httpd -DSSL nobody 1973 0.0 3.9 111304 10072 ? S Sep01 4:21 /usr/sbin/httpd -DSSL nobody 21866 0.0 2.3 109232 6068 ? S Sep04 0:13 /usr/sbin/httpd -DSSL nobody 23804 0.0 5.2 109088 13484 ? S Sep04 0:16 /usr/sbin/httpd -DSSL root 3055 0.0 0.4 3392 1232 pts/0 R+ 08:12 0:00 grep httpComo visto, somente as linhas que continham a string "http" foram retornadas.
Buscando em arquivos
Supondo que procuramos agora por arquivos que contenham a string "http":
$ cd /etc/rc.d
$ grep http *
rc.0:if [ -x /etc/rc.d/rc.httpd ]; then
rc.0: /etc/rc.d/rc.httpd stop
rc.6:if [ -x /etc/rc.d/rc.httpd ]; then
rc.6: /etc/rc.d/rc.httpd stop
rc.K:if [ -x /etc/rc.d/rc.httpd ]; then
rc.K: /etc/rc.d/rc.httpd stop
rc.M:if [ -x /etc/rc.d/rc.httpd ]; then
rc.M: . /etc/rc.d/rc.httpd start
rc.inet2:# See the HOWTOs on http://www.netfilter.org/ for documentation on
rc.modules:# USB scanner support (requires patched SANE from http://fachschaft.cup.uni-muenchen.de/~neukum/scanner.html):
rc.modules:# for telnet and http, for instance. But, the system breaks down when you
rc.wireless.conf:# http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html
Além disso, o comando poderia conter a flag -R:
$ grep http * -R
(Para procurar recursivamente nos diretórios)
Espero que essa dica seja útil!
[]'s
Ragen
eu ainda sugiro outra coisa:
$ man grep
té mais