Friday, April 4, 2008

grep example linux with php

Introduction
grep is a utility to look for specific strings in files, making a search line by line, you can also use regular expression instead of a string.

Usage

grep [options] pattern [file-list]

grep output with no options are all the lines that contain the pattern in the given file.

Some of its more useful options are:

-v
Reverse the behavior of grep and make it list the lines that do not contain the pattern
-n
Shows the line where the pattern is (or is not using the -v option) together with the line number
-c
Displays the number of lines that contains the pattern in the give file(s)
-i
Makes grep to show as a match no matter the case, (lower or upper) so it is no more case sensitive

Some examples of these options are:

Having the file grep.txt (Which the man page of grep listed below)

grep -c very grep.txt
1

grep -c in grep.txt
181

grep -i email grep.txt
Email bug reports to bug-gnu-utils@gnu.org. Be sure to include the

grep -i -n email grep.txt
408: Email bug reports to bug-gnu-utils@gnu.org. Be sure to include the

It is also great for debugging, i.e.:

If you are trying to find a message that comes from dhcpd in your /var/log/messages file, but it has too much info on it, you can use combined with tail to have something like this.

tail -f /var/log/messages | grep dhcp

You will now see on the screen only the messages that contain the string "dhcp" no matter how many messages could be arriving your messages file.



<?
exec('grep -c "continuously" ch1.2.html',$out,$res);
echo "<pre>";
print_r($out)
echo "</pre>";
?>

No comments:

Post a Comment