grep or Egrep or awk filters two or more keywords | using grep to match "and" or "or" mode

Source: Internet
Author: User
Tags lowercase egrep
grep or Egrep or awk filters two or more keywords | using grep to match "and" or "or" mode 2011-06-23 14:06

grep or Egrep or awk filters two or more keywords:
Grep-e ' 123|abc ' filename/find the line containing 123 or ABC in the file (filename)
Egrep ' 123|abc ' filename//With EGREP can also be achieved
How awk '/123|abc/' filename/awk is implemented
use grep to match "and" or "or" mode:
g R e p The general format is:
Code:grep [options] basic Regular expression [file]
Here the basic regular expression can be a string. Single quote double quotation mark
When you enter a string parameter in the G r e P command, it is best to enclose it in double quotes.
You should use single quotes when calling pattern matching.

For example: "M y s t r i n g". There are two reasons for this, one is to avoid being misunderstood as the S H e l command, and the second is a string that can be used to look up multiple words.
When you call a variable, you should also use double quotes, such as: G R E P "$ M Y VA r" file name, and if not, no results will be returned.


The common G r e p options are:

QUOTE:-C outputs only the count of matching rows.
-I is case-insensitive (applies to given only).
-H does not display file names when querying multiple files.
-l outputs only file names that contain matching characters when querying multiple files.
-N Displays matching rows and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V Displays all rows that do not contain matching text. Before starting the discussion, Sir, into a file, insert a piece of text, and add the < Ta B > key after each column, and the vast majority of the G R E P Command example will take this as an example, named D a t A. . F. Generate a file, D a t A. The record structure of F is as follows:

QUOTE: 1th column: City location number.
2nd Column: Month.
3rd column: Store the code and the year of the library.
4th Column: Product code.
The 5th column: the product unified price.
6th column: identification number.
7th column: Qualified quantity. The contents of the document are as follows:

CODE: $ cat DATA.F
Dec 3bc1977 lpsx 68.00 lvx2a 138
483 Sept 5ap1996 USP 65.00 lvx2c 189
The Oct 3zl1998 lpsx 43.00 kvm9d 512
219 Dec 2cc1999 CAD 23.00 PLV2C 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 644
216 Sept 3zl1998 USP 86.00 kvm9e 2341. Query Multiple Files
Query the word "sort it" in all files

CODE: $ grep "Sort It" *2, line matching
1 Displays the text containing the "4 8" string:

CODE: $ grep "DATA.F"2 The total number of output matching rows

CODE: $ grep-c "DATA.F"
4 G R E p returns the number 4, indicating that there are 4 rows containing the string "4 8".

3) Number of lines
Shows the number of rows that meet the matching pattern:

CODE: $ grep-n "DATA.F" the number of rows in the output first column followed by each matching row containing 4 8.

4 show unmatched rows
Show all rows that do not contain 4 8

CODE: $ grep-v "DATA.F"5) Exact Match
As you may have noticed, in the previous example, the string "4 8" was extracted, and the result contains other strings containing "4 4", such as 4 8 8 and 3 4 8, which should actually extract the rows that contain only 4 8.
A more efficient way to extract exact matches using G R e p is to add \ > After the string is extracted. Suppose you now extract an exact 4 8 method, as follows:

CODE: $grep "48\>" data.f

QUOTE: Another way I tried, like I couldn't:
Note there is a < Ta B > key after extracting the string in each matching pattern, so you should do the following:
< Ta b > says click T a b key.
$grep "48<tab>" data.f6) Case Sensitive
By default, G R e P is case sensitive, and you must use the-I switch if you want to query case insensitive strings. In D a t A. F file has the month character S E P T, both uppercase and lowercase, to get this string case insensitive query, the method is as follows:

CODE: $grep-i "DATA.F"grep and regular Expressions

Use regular expressions to add pattern matching to some rules, so you can add more choices to the extraction information. It is best to enclose a regular expression in single quotes, which prevents the proprietary mode used in G R e p from being confused with the special ways of some S H l l commands.

1. Mode range
A city location with code 4 8 4 and 4 8 3 can be used to specify a string range.

CODE: $ grep "48[34]" data.f
483 Sept 5ap1996 USP 65.00 lvx2c 189
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 6442, does not match the beginning of the line
Make the beginning of the line not 4 or 8, you can use the ^ notation in square brackets.

CODE: $ grep "^[^48]" data.f
219 Dec 2cc1999 CAD 23.00 PLV2C 68
216 Sept 3zl1998 USP 86.00 kvm9e 234if it's a string

CODE: $ grep-v "^[^48]" data.f3, set the case
Use-I switch can mask the case sensitivity of the month s E P t

CODE: [Sam@chenwy sam]$ grep-i "Sept" DATA.F
483 Sept 5ap1996 USP 65.00 lvx2c 189
216 Sept 3zl1998 USP 86.00 kvm9e 234 You can also extract all the information for each row containing S E p T and S E P T in a different way [] mode.

CODE: [Sam@chenwy sam]$ grep ' [ss]ept ' DATA.F if you want to extract all the months that contain S E P T, regardless of their case, and this row contains string 483, you can use the pipe command, the symbol ' | '. Output of the left command as "|" The input to the right command. Examples are as follows:

CODE: [Sam@chenwy sam]$ grep ' [ss]ept ' DATA.F | grep 48
483 Sept 5ap1996 USP 65.00 lvx2c 189 You do not have to place the file name in the second G r e P command because its input information comes from the output of the first G r E p command

4. Match any character
If you extract all the code that begins with a K and ends with D, you can use the following methods because the code is known to be 5 characters long:

CODE: [Sam@chenwy sam]$ grep ' K ... D ' DATA.F
The Oct 3zl1998 lpsx 43.00 kvm9d 512
483 may 5pa1998 USP 37.00 kvm9d 644 makes a slight change to the above code, the first two are uppercase letters, the middle two is arbitrary, and ends with C:

CODE: [Sam@chenwy sam]$ grep ' [A-z] ... C ' DATA.F
483 Sept 5ap1996 USP 65.00 lvx2c 189
219 Dec 2cc1999 CAD 23.00 PLV2C 68
484 Nov 7pl1996 CAD 49.00 plv2c 2345. Date Inquiry
A common query pattern is a date query. Check all records starting with 5 at 1 9 9 6 or 1 9 9 8. Use Mode 5. . 1 9 9 [6, 8]. This means that the first character is 5, followed by two points, followed by 1 9 9, and the remaining two digits are 6 or 8.

CODE: [Sam@chenwy sam]$ grep ' 5..199[6,8] ' DATA.F
483 Sept 5ap1996 USP 65.00 lvx2c 189
483 may 5pa1998 USP 37.00 kvm9d 6446. Range Combination
You must learn to use [] to extract information. Suppose you want to get the city code, the first character is 0-9, the second character is between 0 and 5, the third character is between 0 and 6, and the following patterns are used to achieve it.

CODE: [Sam@chenwy sam]$ grep ' [0-9][0-5[0-6] ' DATA.F
Dec 3bc1977 lpsx 68.00 lvx2a 138
483 Sept 5ap1996 USP 65.00 lvx2c 189
The Oct 3zl1998 lpsx 43.00 kvm9d 512
219 Dec 2cc1999 CAD 23.00 PLV2C 68
484 Nov 7pl1996 CAD 49.00 plv2c 234
483 may 5pa1998 USP 37.00 kvm9d 644
216 Sept 3zl1998 USP 86.00 kvm9e 234 Returns a lot of information here, there is something you want and you don't want. Reference mode, which returns the result is correct, so here

CODE: [Sam@chenwy sam]$ grep ' ^[0-9][0-5][0-6] ' DATA.F
219 Dec 2cc1999 CAD 23.00 PLV2C 68
216 Sept 3zl1998 USP 86.00 kvm9e 234 This allows you to return an expected correct result.

7. The probability of the pattern appearing
Extract all rows that contain the number 4 repeat at least two times, as follows:

CODE: [Sam@chenwy sam]$ grep ' 4\{2,\} ' DATA.F
483 may 5pa1998 USP 37.00 kvm9d 644 The above syntax indicates that the number 4 repeats at least two times, noting the difference between a borderless character.
Similarly, the extraction record contains the number 9 9 9 (three 9), as follows:

CODE: [Sam@chenwy sam]$ grep ' 9\{3,\} ' DATA.F
219 Dec 2cc1999 CAD 23.00 PLV2C 68 If you want to query for all rows that have a recurring number of occurrences, the syntax is as follows, and the number 9 repeats two times or three times:

CODE: [Sam@chenwy sam]$ grep ' 9\{3\} ' DATA.F
219 Dec 2cc1999 CAD 23.00 PLV2C 68
[Sam@chenwy sam]$ grep ' 9\{2\} ' DATA.F
483 Sept 5ap1996 USP 65.00 lvx2c 189
The Oct 3zl1998 lpsx 43.00 kvm9d 512
219 Dec 2cc1999 CAD 23.00 PLV2C 68
484 Nov 7pl1996 CAD 49.00 plv2c 234 sometimes to query for repeated occurrences in a range, such as numbers or letters repeated 2 to 6 times, the following example matches the number 8 repeats 2 to 6 times, and ends with 3:

CODE: [Sam@chenwy sam]$ Cat MyFile
83
888883
8884
88883
[Sam@chenwy sam]$ grep ' 8\{2,6\}3 ' myfile
888883
888838. Use grep to match "and" or "or" mode
G r e p command plus-e argument, this extension allows you to use extended pattern matching. For example, to extract the city code for 2 1 9 or 2 1 6, the following methods are used:

CODE: [Sam@chenwy sam]$ grep-e ' 219|216 ' DATA.F
219 Dec 2cc1999 CAD 23.00 PLV2C 68
216 Sept 3zl1998 USP 86.00 kvm9e 2349, blank line
Use a combination of ^ and $ to query for empty rows. Use the-c parameter to display the total number of rows:

CODE: [Sam@chenwy sam]$ grep-c ' ^$ ' myfile uses-n parameters to show the actual line:

CODE: [Sam@chenwy sam]$ grep-c ' ^$ ' myfile10. Matching special characters
Query for characters that have special meaning, such as $. ' " * [] ^ | \ + ? , you must add \ before a specific character. Suppose you want the query to contain the "." , and the script is as follows:

CODE: [Copy to clipboard] [Sam@chenwy sam]$ grep ' \. ' MyFile or a double quote:

CODE: [Sam@chenwy sam]$ grep ' \ ' myfile in the same way, such as to query the filename c o n f t r o L. c o n F (This is a configuration file), the script is as follows:

CODE: [Sam@chenwy sam]$ grep ' conftroll\.conf ' myfile11. Format file name of query
Use regular expressions to match an arbitrary file name. There is a standard naming format for text files in the system. Typically up to six lowercase characters, followed by a period, followed by two uppercase characters.

CODE: [Sam@chenwy sam]$ grep ' ^[a-z]\{1,6\}\. [a-z]\{1,2\} ' filename This writing I do not know if there is no mistake: oops:: oops:: oops:

12 Query IP address
To see n N. N. n N a network address, if you forget the rest of the second part, you only know two periods, such as N N. n N. .。 To extract all of the nnn.nnn IP addresses, use [0-9] \ {3 \} \. [0-0 \ {3 \} \. The implication is that any number appears 3 times, followed by a period, followed by any number 3 times, followed by a period.

CODE: [0-9]\{3\}\. [0-9]\{3\}\.]1. Class name

The G R e p allows the use of an international character pattern matching or matching pattern in the class name form.
Regular expressions with class names and their equivalent regular expression classes equivalent regular expressions

QUOTE: [[: U P p e r:]] [A-z] [[: A L n u m:]] [0-9 a-za-z]
[[: L o W E r:]] [A-z] [[: s p a c e:]] space or t a B key
[[: D I g i t:] [0-9] [[: A L P h a:]] [A-Z] Example: Take the beginning of 5, followed by at least two uppercase letters:

Code: $grep ' 5[[:upper:]][[:upper]] ' data.f to take all product codes ending with P or D:

Code:grep ' [[: Upper:]][[:upper:]][p,d] ' DATA.F2, the use of wildcard * matching mode

CODE: $cat testfile
Looks
Likes
Looker
Long try the following:

Code:grep "l.*s" testfile such as querying a word at the end of a line, try the following mode:

Code:grep "ng$" testfile this will query all files for all the lines that contain the word ng.

3, System grep

File passwd


CODE: [Root@linux_chenwy sam]# grep "Sam"/etc/passwd
Sam:x:506:4::/usr/sam:/bin/bash the above script query/E t c/p a s W d file contains Sam string

If you mistakenly enter the following script:

CODE: [Root@linux_chenwy sam]# grep "Sam"/etc/password
grep:/etc/password: No that file or directory will return the G r e P command error code ' no such file or directory '.
The above results indicate that the input filename does not exist and the G r E p command-s switch is used to mask the error message.
Return to the command prompt without the error message that the file does not exist.

CODE: [Root@linux_chenwy sam]# grep-s "Sam"/etc/password if the G r E P command does not support the-s switch, the following command can be used instead:

CODE: [Root@linux_chenwy sam]# grep "Sam"/tec/password >/dev/null 2>&1 script meaning is to match command output or error (2 > $1) and output the result to the system pool. Most system administrators say/d e v/n u l l for bit pool, it doesn't matter, it can be seen as a bottomless pit, there is no out, will never be filled.

The above two examples are not good, because the purpose here is only to know if the query was successful.

To save the query results of the G r e P command, you can redirect the command output to a file.

CODE: [Root@linux_chenwy sam]# grep "Sam"/etc/passwd >/usr/sam/passwd.out
[Root@linux_chenwy sam]# Cat/usr/sam/passwd.out
The Sam:x:506:4::/usr/sam:/bin/bash script redirects the output to the directory/T m p below the file p a s W d. o u T.

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.