Tuesday, July 12, 2011

The Grep Filter.!

Search content of text files for matching patterns. It is definitely worth learning at least the basics of this command.
A simple example. The command:
cat * | grep my_word | more
will search all the files in the current working directory (except files starting with a dot) and print the lines which contain the string "my_word".
A shorter form to achieve the same may be:
grep my_word * |more
The patterns are specified using a powerful and standard notation called "regular expressions".
There is also a "recursive" version of grep called rgrep. This will search all the files in the current directory and all its subdirectories for my_word and print the names of the files and the matching line:
rgrep -r my_word . | more

No comments:

Post a Comment