One of the most used commands when you administer a UNIX or Linux command line is certainly `grep`.
For those not familiar, I will simply say that it is an efficient and highly flexible script that can find regular expressions in a file or a text output.
The command name is in fact stands for "global regular expression-print".
Everything else can be found with the usual `man grep`.
For those who usually use grep for administrative tasks every day, I collect here a number of examples of use a little 'more evolved and - maybe - unusual.
May be useful to you as they were to me on numerous occasions.
Find occurrences of the sequence here, here in court AND file.txt:
grep "qui.*quo.*qua" ./file.txt
Find occurrences of OR here OR here in court file.txt:
grep -P 'qui|quo|qua' ./file.txt
Verifying that an email address is formatted correctly:
echo "info@test.eu" | grep -Ei '\b[a-z0-9]{1,}@*\.(it|eu|com|net|org|tv)\b'
Find the word here in a case in-sensitive in. / File.txt:
grep -iw "is" demo_file
Find email addresses in test.txt formatted as <qwerty.qwerty@qwerty.com> enclosure and removes characters (<and>):
grep -o '<.*@.*\.*>' ./test.txt | tr -d '<>'
Find multiple occurrences of the OR here OR quo here in a text string:
echo -e "1) qui quo qua\n2) quo qui qua\n3)qui quo qui\n4) qua quo qui\n" | grep -E '(qui|quo|qua).*\1'
Check if the string passed through a pipe to IP address or not (try passing an ill-formed IP address):
echo "192.168.123.123" | grep -E '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
Show all rows dell'httpd. Conf omitting the commented instructions:
grep -v -E '^\#|^$' /etc/apache2/httpd.conf
Assuming that the test.sh script contains the following instructions:
#!/bin/bash
contatore=1
until [ $contatore -gt 10 ]; do
echo contatore $contatore
let contatore+=1
done
The following command ... find the line 4 and the 3 following lines:
./test.sh | grep -A 3 -i "contatore 4"
Identifies the following command ... instead all lines except those containing 4 and 6:
./test.sh | grep -v -e "4" -e "6"
The official website of the project is located at grep http://gnu.org/software/grep/ .