Linux Text processing the Three Musketeers grep

Source: Internet
Author: User
Tags egrep


There are three major text processing tools under Linux, namely Grep,sed,awk. Now to summarize the use of grep.

grep (Global search Regular expression (RE) and print out of the line, full

Search for regular expressions and print them out) is a powerful text search tool that can use regular tables

Search for text and print out the matching lines. The grep family of Unix includes grep, Egrep, and

Fgrep Egrep and Fgrep commands are only a small difference from grep. Egrep is the extension of grep, which supports more

Multiple re metacharacters, Fgrep is fixed grep or fast grep, they treat all the letters as single

Words, that is to say, metacharacters in regular expressions are expressed back to their literal meanings, no longer special. Linux

Use the GNU version of grep. It is more powerful and can be used with the-G,-e,-f command line Options

Functions of Egrep and Fgrep.

grep works in such a way that it searches for a string template in one or more files. If the template includes spaces, it must be referenced, and all strings after the template are treated as filenames. The results of the search are sent to the screen without affecting the contents of the original file.

grep can be used for Shell scripting, because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can use these return values to do some automated text processing work.


matching mode selection:
-E,--extended-regexp extended regular Expression Egrep
-F,--fixed-strings a collection of string separated by a newline character fgrep
-G,--basic-regexp Basic Regular
-P, Perl regular called by--perl-regexp
-E,--regexp=pattern back root regular mode, default None
-F,--file=file get the matching pattern from the file
-I,--ignore-case case insensitive
-W,--word-regexp matches whole word
-X,--line-regexp matches the entire row
-Z,--null-data a 0-byte data row, but not a blank line
-S,--no-messages do not display error messages
-V,--invert-match display unmatched rows (reverse lookup)


Input Control:
-M, maximum number of--max-count=num matches
-B,--byte-offset prints the block number where the line is printed before the line.
-N,--line-number displays the line number of the match
--line-buffered Refresh output per row
-H,--with-filename displays the matching file name prefix when searching for multiple files
-H,--no-filename does not display a matching file name prefix when searching multiple files
-Q,--quiet,--silent don't show anything.
--binary-files=type assumes the type of the binary file;
TYPE can be ' binary ', ' text ', or ' without-match '
-A,--text match binary stuff
-I does not match binary stuff
-D,--directories=action directory operation, read, recursive, skip
-D,--devices=action set to device, FIFO, pipe operation, read, skip
-R,-R,--recursive recursive invocation
--include=pattern only find files that match File_pattern
--exclude=pattern skipping files and directories that match File_pattern
--exclude-from=file skip all files except file
-L,--files-without-match matches multiple files, displays mismatched filenames
-L,--files-with-matches matches multiple files, displays matching filenames
-C,--count shows how many times it matches
-Z,--null print empty characters at the end of file


File Control:
-B, the--before-context=num print match itself and several previous lines are controlled by NUM
-A, the--after-context=num print match itself and several subsequent lines are controlled by NUM
-C,--context=num prints the match itself and subsequently, the previous lines are controlled by NUM
-num root-C uses the same
--color[=when],
--colour[=when] Use the flag to highlight the matching string;

-U,--binary use the flag to highlight the matching string;


Example 1:

Test file
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin
Qemu:x:107:107:qemu User:/:/sbin/nologin
Abrt:x:173:173::/etc/abrt:/sbin/nologin
Pulse:x:171:171:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin
Gdm:x:42:42::/var/lib/gdm:/sbin/nologin
Gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin
Tcpdump:x:72:72::/:/sbin/nologin
Willis:x:1000:1000:willis:/home/willis:/bin/bash
Print:x:1001:1001::/home/print:/bin/bash

1. Match the row containing the root
[[email protected] test1]# grep root file
Root:x:0:0:root:/root:/bin/bash

2. Match lines starting with root or starting with Willis
[email protected] test1]# cat file |grep ' ^\ (root\|willis\) '
Root:x:0:0:root:/root:/bin/bash
Willis:x:1000:1000:willis:/home/willis:/bin/bash

3. Match rows that do not start with bin and display line numbers
[email protected] test1]# Cat File | GREP-V-N ^bin
1:root:x:0:0:root:/root:/bin/bash
3:rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
4:nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin
5:qemu:x:107:107:qemu User:/:/sbin/nologin
6:abrt:x:173:173::/etc/abrt:/sbin/nologin
7:pulse:x:171:171:pulseaudio System Daemon:/var/run/pulse:/sbin/nologin
8:gdm:x:42:42::/var/lib/gdm:/sbin/nologin
9:gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
10:sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin
11:tcpdump:x:72:72::/:/sbin/nologin
12:willis:x:1000:1000:willis:/home/willis:/bin/bash
13:print:x:1001:1001::/home/print:/bin/bash

4. Matches the number of displays, does not display the content
[email protected] test1]# Cat File | Grep-c Root
1
[email protected] test1]# Cat File | Grep-c bin
13
[email protected] test1]# Cat File | Grep-c Willis
1

5. Match Show Root 3 lines
[email protected] test1]# Cat File | Grep-a 3 Root
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Rpcuser:x:29:29:rpc Service User:/var/lib/nfs:/sbin/nologin
Nfsnobody:x:65534:65534:anonymous NFS User:/var/lib/nfs:/sbin/nologin

This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1845868

Linux Text processing the Three Musketeers grep

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.