Total Pageviews

Tuesday 3 May 2016

grep搜寻指定的字符串

grep -ri "some_string" /to/path/

在/to/path/目录中,递归搜寻指定的字符串some_string。
-r表示递归,-i表示忽略字符串的大小写。

grep -i "some_string" some_file则是在some_file中,搜寻指定的字符串some_string.

related post: http://briteming.blogspot.com/2013/02/grep.html
----------------
Find 这个命令会在给定位置搜寻与条件匹配的文件。
你可以使用find folder-to-search -name filename的-name选项来进行区分大小写的搜寻.
               find folder-to-search -iname filename来进行不区分大小写的搜寻。
------------

grep的用法


The grep command which stands for “global regular expression print,” processes text line by line and prints any lines which match a specified pattern. The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. Grep is considered to be one of the most useful commands on Linux and Unix-like operating systems. grep is a powerful file pattern searcher in Linux. If it is not installed on your system, you can easily install it via your package manager (apt-get on Debian/Ubuntu and yum on RHEL/CentOS/Fedora). For installing grep in your system, please use the following command.
$ sudo apt-get install grep         #Debian/Ubuntu
$ sudo yum install grep             #RHEL/CentOS/Fedora

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified. The grep has no limits on input line length other than available memory, and it can match arbitrary characters within a line. If the final byte of an input file is not a newline, grep silently supplies one. Since newline is also a separator for the list of patterns, there is no way to match newline characters in a text.

Using grep for search files.
To search /etc/passwd file for the user harry, enter the following command.
$ grep harry /etc/passwd
Sample outputs:
harry:x:1000:1000:harry,,,:/home/harry:/bin/ksh
You can force grep to ignore word case i.e match harry, HARRY, Harry and all other combination with the -i option:
$ grep -i “harry” /etc/passwd
If you want to search for a word, and avoid matching substrings use ‘-w ‘option. Just doing a normal search will show all the lines. The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.
$ grep -i “is” samplefile
THIS LETTER IS THE 1ST UPPER CASE LETTER IN THIS LINE.
this letter is the 1st lower case letter in this LINE.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.
grep -iw “is” samplefile
THIS LETTER IS THE 1ST UPPER CASE LINE IN THIS FILE.
this letter is the 1st lower case letter in this line.
Two lines above this line is empty.
And this is the last line