Centos – How to grep or find exactly what i am looking for

centosfindgrep

How can i search a word called "Onderh"? Advanced search i would like to do such as: starting line with "Onderh" or line contain "Onderh".

Note: Local Backup directory, contain every days a snapshot of MySQL "database.sql (file size: 81902272)". Inside that file i need to apply the search.

[me@www win]$ ls
2011-03-01  2011-03-04  2011-03-07  2011-03-10  2011-03-13  2011-03-16  2011-03-19  2011-03-22  2011-03-25  2011-03-28  2011-03-31  2011-04-03  2011-04-06
2011-03-02  2011-03-05  2011-03-08  2011-03-11  2011-03-14  2011-03-17  2011-03-20  2011-03-23  2011-03-26  2011-03-29  2011-04-01  2011-04-04  2011-04-07
2011-03-03  2011-03-06  2011-03-09  2011-03-12  2011-03-15  2011-03-18  2011-03-21  2011-03-24  2011-03-27  2011-03-30  2011-04-02  2011-04-05  2011-04-08

[me@www 2011-04-08]$ ll
total 80092
-rw-r--r--  1 root root 81902272 Apr  8 01:00 database.sql
drwxr-xr-x 99 root root    12288 Apr  8 01:00 etc
drwxr-xr-x  4 root root     4096 Apr  8 01:00 home
drwxr-xr-x 10 root root     4096 Apr  8 01:00 html
drwxr-xr-x 14 root root     4096 Apr  8 01:00 postfixadmin-2.3

Best Answer

If you want to check across all of those directories, try:

[me@www win]$ grep -n ^Onderh */database.sql

to find lines (the -n flag shows line number) starting with Onderh. The ^ is a special character to anchor the match to the start of the line - get rid of it if you want to find it anywhere in the line.

Ack could also be worth looking into if you have more complicated requirements. For example if the sql files you were interested in weren't so well organised, ack would be able to search within only .sql files in an entire directory structure.

Related Question