Linuxgrep command usage

Source: Internet
Author: User
First, create the demo file demo_file that we need to use to practice the grep command. $ Snapshots...
First, create the demo file demo_file that we need to use to practice the grep command. $ Cat demo_fileTHIS line is the 1ST upper case line in this file. this line is the 1st lower case line in this file. this Line Has All Its First Character Of The Word With Upper Case. www.2cto.com Two lines above this line is empty. and this is the last line.
1. The basic usage of searching for the specified string grep from a single file is as follows. Syntax: grep "literal_string" filename $ grep "this" demo_filethis line is the 1st lower case line in this file. Two lines above this line is empty. And this is the last line.
2. search for the specified string syntax in multiple files: grep "string" FILE_PATTERN. first copy demo_file to demo_file1. Grep results include the file name before the row that meets the criteria. When the file name contains metacharacters, linux shell will input all matched files to grep. $ Cp demo_file demo_file1 www.2cto.com $ grep "this" demo _ * demo_file: this line is the 1st lower case line in this file. demo_file: Two lines above this line is empty. demo_file: And this is the last line. demo_file1: this line is the 1st lower case line in this file. demo_file1: Two lines above this line is empty. demo_file1: And this is the last line.
3. use grep-I for case-insensitive search syntax: grep-I "string" FILE is also a basic usage. the search strings are case-insensitive. Therefore, the following example matches "", "THE" and "". $ Grep-I "the" demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. this Line Has All Its First Character Of The Word With Upper Case. and this is the last line.4. use the regular expression syntax: grep "REGEX" filename. this is a useful feature if you can effectively use regular expressions. In the following example, search for all strings ending with "lines" and ending with "empty", for example, search for "lines [any word] empty" and ignore case sensitivity. $ Grep-I "lines. * empty" demo_fileTwo lines above this line is empty. how many repeated operations do regular expressions follow? Match up to one www.2cto.com * match zero times or any multiple times + match more than once {n} match n times {n,} match at least n times {, m} match up to m Times {n, m} matches n to m times 5. use grep-w to search for the entire word, instead of using the-w option to search for some strings in the word, and avoid searching for some strings in the word. Search for "is" in the following example ". If the-w option is not added, all rows including "is", "his", and "this" are displayed. $ Grep-I "is" demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. 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 uses The-w option. Note that The result does not contain "This Line Has All Its First Character Of The Word With Upper Case", although "This" contains "is ". $ Grep-iw "is" demo_file www.2cto.com this line is the 1ST upper case line in this file. this line is the 1st lower case line in this file. two lines above this line is empty. and this is the last line.6. use grep-A,-B and-C to display the rows before, after, And before and after grep to search for large files, displaying data from multiple rows near matching rows is a useful feature. Create the following file $ cat demo_text4. vim Word Navigation You may want to do several navigation in relation to the words, such as: * e-go to the end of the current word. * E-go to the end of the current WORD. * B-go to the previous (before) word. * B-go to the previous (before) WORD. * w-go to the next word. * W-go to the next WORD. WORD-WORD consists of a sequence of non-blank characters, separat Ed with white space. word-word consists of a sequence of letters, digits and underscores. example to show the difference between WORD and word * 192.168.1.1-single WORD * 192.168.1.1-seven words.6.1: grep-A "string" FILENAME the following example shows the matching row and the following three rows of data $ grep-A 3-I "example" demo_texample to show the difference between WORD and word www.2cto.com * 192.168.1.1 -single WORD * 192.168.1.1 -Seven words.6.2: Show the N-B syntax before matching rows: grep-B "string" FILENAME the following example shows the matching row and the previous two rows of data $ grep-B 2 "single WORD" demo_texample to show the difference between WORD and word * 192.168.1.1-single WORD6.3 show N rows before and after matching rows-C show n rows before and after matching, next n rows of data. $ grep-C 2 "Example" demo_textword-word consists of a sequence of letters, digits and underscores. example to show the difference between WORD and word * 192.168.1.1-single W ORD7. use GREP_OPTIONS to highlight the search string. if you want to highlight the search string in the result, try the following methods. Modify GREP_OPTIONS to highlight the search string. $ Export GREP_OPTIONS = '-- color = Auto' GREP_COLOR = '2017; 8' $ grep this demo_filethis line is the 1st lower case line in this file. two lines above this line is empty. and this is the last line.
8. use grep-r to Recursively search all files. if you want to find all files in the current directory and its subdirectories, use the-r option. For example, $ grep-r "ramesh" * 9. if you use grep-v to perform a mismatch, you can use the-v option to display rows that do not match the search string. The following example shows that the demo_text file does not contain the "go" line $ grep-v "go" demo_text www.2cto.com 4. vim Word Navigation You may want to do several navigation in relation to the words, such as: WORD-WORD consists of a sequence of non-blank characters, separated with white space. word-word consists of a sequence of letters, digits and underscores. example to show the difference between WORD and word * 192.168.1.1-single WORD * 192.168.1.1-seven words.
10. show line syntax that does not match all modes: grep-v-e "pattern"-e "pattern" create the following example file $ cat test-file.txtabcd $ grep-v-e "a"-e "B"-e "c" test-file.txtd www.2cto.com 11. use grep-c to calculate the number of matched rows. Syntax: grep-c "pattern" filename $ grep-c "go" demo_text6: count the number of unmatched rows $ grep-v-c this demo_file412. use grep-l to display only the file name $ grep-l this demo _ * demo_filedemo_file1
13. only matching strings are displayed. by default, the row of matching strings is displayed. you can use the-o option to only display matching strings. This function is useful when regular expressions are used. $ Grep-o "is. * line "demo_file is line is the 1st lower case lineis is the last line14. display the matching location syntax: grep-o-B "pattern" file $ cat temp-file.txt1234512345 $ grep-o-B "3" temp-file.txt0: 36: 3 www.2cto.com note: The above output shows a location not in the row, it is the byte location of the entire file. when grep-n is used for output, the row number is displayed starting from 1 $ grep-n "go" demo_text5: * e-go to the end of the current word.6: * E-go to the end of the current WORD.7: * B-go to the previous (before) word.8: * B-go to the previous (before) WORD.9: * w-go to the next word.10: * W-go to the next WORD.
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.