← Blog

Grep - Búsqueda de Texto

Apuntes y comandos de Grep - Búsqueda de Texto para Linux.

  • Verify a service is running and listening
netstat -antp |grep apache
  • Cut a string by a delimiter, filter results then sort
grep "href=" index.html | cut -d "/" -f 3 | grep "\\." | cut -d '"' -f 1 | sort -u
  • Pick all strings from a binary file
strings <file_name> | grep <searched text>
  • Extract all the lines that contain a string
grep "href=" index.html
  • Search command history
history | grep phrase_to_search_for
  • Using Grep and regular expressions and output to a file
cat index.html | grep -o 'http://\[^"\]\*' | cut -d "/" -f 3 | sort –u > list.txt

Plain text username / password

grep -i user [filename]
grep -i pass [filename]
grep -C 5 "password" [filename]

Searching & processing text:

grep bob wordlist.txt
# search for 'bob' in file

grep -v e wordlist.txt
# search for words that do not have 'e'

grep error /var/log/*.log
# search error in log files

grep error -B 3 -A 2 /var/log/*.log
# -B to print lines before hit
# -A to print lines after hit

sort random.txt
# used to sort contents alphabetically

sort -nr random-numbers.txt
# -n for numbers and -r for reverse order

# uniq filters only adjacent duplicate lines
sort random-words.txt | uniq
# alphabetic, non-dupli output

wc words.txt
# prints lines, words and bytes in file

grep bob words.txt | wc -l
# count no. of occurrences