One day a shell command Linux text content Operation series-grep command detailed _linux Shell

Source: Internet
Author: User
Tags perl regular expression regular expression stdin egrep

From this beginning, it is the text content operation, which differs from the text operation.

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching rows. The grep full name is global Regular Expression Print, which represents the global regular expression version, and its use permissions are all users.

Shell,perl,python is always an expert language for text manipulation, and what we will learn in the future is the shell gimmick--the text operation. The most common one is mentioned below:

Grep

This is a heavyweight of text content that can look up data in thousands of lines of text files based on certain specifications. He can accept regular expressions and wildcard characters.

Example:

1. Command translation a line of text containing a given match_pattern, as I look for pattern in grep.txt

grep pattern Grep.txt

or grep "pattern" grep.txt

2. Match multiple files

grep pattern Grep.txt Grep1.txt

3. Read from stdin

Echo–e "This are a word\nnext line" | grep word

Printing: This is a word

4. grep and Regular expressions

Grep–e "[a-z]+"

Or

Egrep "[a-z]+"

5. Output only the matching part, not the matching forward

The Echo is line. | Greip–o–e "[A-z]+\."

Output: Line

6. Print all rows except the line containing Match_pattern, using the

Grep-v Match_pattern File

7. Statistics text or file contains the word number of a matching string (this interview is often asked)

$grep-C "text" filename

But he's given the number of matching rows, not the number of matches.

If you count the number of matches, you can use the following techniques

$echo –e "1 2 3 4\nhello\n5 6" | Egrep-o "[0-9]" | Wc–l

Printing 1 to 6 | Read numbers from stdin, branch print | Statistics line number

8. Print line number

grep "Text"-N filename

9. Search 2 files, and their line number, the output will print the file name

grep "text" –n s1.txt s2.txt

10. If you only want to know which files have Word pattern

Grep–l Patter S1.txt S2.txt

The list of mismatched files returned by-l

11. You can look up the whole list of words in those files.

grep pattern. -r-n

12 simultaneous multiple matching lookups

The Echo is a line of text | Grep-e "This"-e "line"-O

Print:

This

Line

13 search only in some of the right files

grep "Main ()".-R--include *. {C,cpp}

Search for Main () only in files that end with. C and. cpp

14 Search to exclude certain files

grep "Main ()". -R--exclude "README"

All files with file name "README" excluded

15 print 3 lines before or after match

Seq 10 | grep 5–a 3 ago

Seq 10 | After grep 6–b 3

16 print match before and after 3 lines, while output

Seq 10 | grep 5–c 3

Let's look at the--help.

Copy Code code as follows:

Usage: grep [options] ... pattern [FILE] ...
Find pattern in each FILE or standard input.
The default pattern is a basic regular expression (abbreviated as BRE).
For example: Grep-i ' Hello World ' menu.h main.c

Regular expression Selection and interpretation:
-E,--extended-regexp pattern is an extensible regular expression (abbreviated as ERE)
-F,--fixed-strings pattern is a set of fixed-length strings separated by a break character.
-G,--basic-regexp pattern is a basic regular expression (abbreviated as BRE)
-P,--perl-regexp pattern is a Perl regular expression
-E,--regexp=pattern using pattern for matching operations
-F,--file=file gets pattern from file
-I,--ignore-case ignores case
-W,--word-regexp forced pattern to match only exactly words
-X,--line-regexp force pattern to match only one row exactly
-Z,--null-data a 0-byte data row, but not a blank line

Miscellaneous:
-S,--no-messages does not display error messages
-V,--invert-match select unmatched rows
-V,--version display version information and exit
--help Show this Help and exit
--mmap Ignore backward compatibility

Output Control:
-M,--max-count=num stop after NUM matches
-B,--byte-offset print the byte offset with output lines
-N,--line-number print line number with output lines
--line-buffered flush output on every line
-H,--with-filename print the file name for each match
-H,--no-filename suppress the file name prefix on output
--label=label use label as the standard input file name prefix
-O,--only-matching shows only the part of a row that matches pattern
-Q,--quiet,--silent does not show all output
--binary-files=type assumes the type of the binary file;
TYPE can be ' binary ', ' text ', or ' without-match '
-A,--text equivalent to--binary-files=text
-I equivalent to--binary-files=without-match
-D,--directories=action the way the directory is operated;
ACTION can be ' read ', ' recurse ', or ' skip '
-D,--devices=action operation Equipment, first into the first out of the queue, the way the socket;
ACTION can be ' read ' or ' Skip '
-R,-R,--recursive equivalent to--directories=recurse
--include=file_pattern only find files that match File_pattern
--exclude=file_pattern Skip matching file_pattern files and directories
--exclude-from=file skips all files except file
--exclude-dir=pattern skips all directories that match pattern.
-L,--files-without-match only print file names that do not match files
-L,--files-with-matches only print file names that match files
-C,--count only prints the number of matching rows in each file
-T,--initial-tab header tabs delimited (if necessary)
-Z,--null prints the null character at the end of the file

File control:
-B,--before-context=num print NUM lines starting with text
-A,--after-context=num prints NUM lines ending with text
-C,--context=num print output text NUM line
-num equal to--context=num
--color[=when],
--colour[=when] uses flags to highlight matching strings;
When it can be ' always ', ' Never ' or ' auto '
-U,--binary do not clear the CR characters at the end of the line (MSDOS mode)
-U,--unix-byte-offsets when CR character does not exist, report byte offset (MSDOS mode)

' Egrep ' is ' grep-e '. ' Fgrep ' is ' grep-f '.
Direct use of ' egrep ' or ' fgrep ' is no longer a good line.
Without the file parameter, or file is-, the standard input is read. If less than two FILE parameters
You will use the-H parameter by default. If any row is selected, the exit status is 0, otherwise 1;
If an error is generated and the-Q argument is not specified, the exit status is 2.

Most of the features have been posted in the example, my blog post, the main reference book, "Linux shell script Raiders"

If you think it is more time to learn more, you may wish to buy this book directly.

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.