Shell Foundation 8: Text Filter Tool (GREP)

xiaoxiao2021-03-06  67

For more than a grep, you can find GREP, EGREP, FGREP in Linux, which is approximately as follows: * Grep: Traditional GREP programs, only output the RE string string without parameters The common parameters are as follows: -v: Reverse mode, only the "Do not contain" RE string strings. -R: recursive mode, can handle files in all level subdirectory. -Q: Silent mode, no output any Results (except STDERR. Common to get Return Value, in line with true, otherwise false.) -I: ignore the case. -W: Ontario comparison, similar / . -N: Simultaneous output line number. -c: Only the number of rows that meet the compliance. -L: Only the compliant file name is output. -o: Only the string complies with the RE. (GNU new version is unique, not all versions are supported.) E: Switch into egrep. * EGREP: For the expansion version of GREP, improved many traditional GREP's operating. Compared to the side: - GREP does not support? With these two modifier, but eGrep can. - GREP Does "or a" alignment of A | B or (ABC | XYZ), but EGREP can be. - GREP When processing {n, m}, you need to process / {and /}, but EGREP does not need Sometimes ... I personally suggest that you can use EGREP without grep ... ^ _ ^ * fgrep: Not Re-processing, the expression is only general string processing, all META is lost.

G R E P General Format is:

Code: GREP [Option] Basic regular expression [File] The basic regular expression here can be a string.

When the single quota double quotes entered the string parameter in the G R e-page, it is best to enclose it with a double quote. Single quotes should be used when the mode match is matched. For example: "M Y S T R I Ng". There are two reasons for this, one is to prevent misunderstanding of the S H E L command, and the other is a string that can be used to find multiple words. When the variable is called, double quotation marks should also be used, such as the G R e P "$ m y va r" file name, if not, there will be no return results. Commonly used G r e p options are:

Quote: -c only outputs the count of matching lines. -i is not case sensitive (only for single characters). -H Query multi-files do not display the file name. -l Queries only the file name containing the matching character is output when the multi-file is queued. -n Displays the matching line and the line number. -s does not display error messages that do not exist or have no text. -v displays all rows that do not contain matching text.

Before you start discussing, you will become a file, insert a piece of text, and add the key after each column, the vast majority in the G R e P command example will be used as an example, which is named D a t a. F. Generate a file, the record structure of D A T a. F is as follows:

Quote: Part 1: Urban location number. Line 2: Month. Level 3: Storage code and outbound year. Column 4: Product code. Column 5: The product is unified. Column 6: Identification number. Column 7: The number of passes.

The document content is as follows:

Code: $ cat data.f 48 Dec 3BC1977 LPSX 68.00 LVX2A 138 483 Sept 5AP1996 USP 65.00 LVX2C 189 47 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_________________1, query multiple files in all files "Sort IT"

Code: $ grep "sort it" * 2, row match 1) Show text containing "4 8" strings:

Code: $ grep "48" data.f2) The total number of output matches

Code: $ grep -c "48" data.f 4G R e p Returns the number 4, indicating that there are 4 lines containing the string "4 8". 3) The number of lines display all the number of lines that meet the matching mode:

Code: $ grep -n "48" DATA.F The number of rows is output first, followed by each matching line of 4 8. 4) Displaying a non-matched line display all rows that do not contain 4 8

Code: $ grep -v "48" DATA.F5) Exact match may have noticed that in the previous example, the string "4 8" is extracted, and the return result contains "4 8, 4 8 3, etc. "Other strings, in fact, should actually extract only the lines of 4 8. A more efficient way to extract exact match using G R e p is to add /> after extracting strings. Assume that 4 8 is now extracted now, the method is as follows:

Code: $ GREP "48 />" DATA.F

Quote: Another way I tried it, it seems that it is not possible: Note that there is a key after extracting the string in each match mode, so it should be operated as follows: indicates that click the T A B key. $ GREP "48 " DATA.F6) Sensitive Bymount Sensitive By default, G R e P is sensitive. If you want to query the case where you are not sensitive string, you must use the - I switch. In the D A T a. F file, there is a month character S e p t, both uppercases and lowercase, to get this string case insensitive query, the method is as follows:

Code: $ grep -i "48" data.fgrep and regular expressions use regular expressions to enable mode matching to some rules, so you can add more options in the extracted information. It is best to use a single quotes when using regular expressions, which prevents the proprietary mode used in G R E P from confuses the special way of some S H E L command. 1. The mode range extracts the code of 4 8 4 and 4 8 3, and [] can be used to specify the range of strings.

Code: $ grep "48 [34]" DATA.F 483 SEPT 5ap1996 USP 65.00 LVX2C 189 49.00 PLV2C 234 483 May 5pa1998 USP 37.00 KVM9D 6442, mismatched line first make the head not 4 or 8, can be Use the ^ marker in parentheses.

Code: $ grep "^ [^ 48]" DATA.F 219 DEC 2CC1999 CAD 23.00 PLV2C 68 216 SEPT 3ZL1998 USP 86.00 KVM9E 234 If it is a string 48 code: $ grep -v "^ [^ 48]" DATA.F3, Set sensitive use - I switches can block the case sensitive for month S EPT

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 can also use another way [] mode to extract each line contains S EPT and SEPT All information.

Code: [SAM @ chenwy sam] $ grep '[SS] EPT' DATA.F If you want to extract all the months containing S EPT, no matter whether it is small, and this line contains strings 483, you can use the pipeline command, ie symbol " "The output of the left command is the input of" | "right command. For example, as follows:

Code: [SAM @ chenwy sam] $ grep '[SS] EPT' DATA.F | GREP 48 483 SEPT 5AP1996 USP 65.00 LVX2C 189 Do not need to put the file name in the second GREP command, because its input information comes from the first The output of a grep command 4, matching any character, if extracted with K, all code ended with D, the following method can be used because the code length is 5 characters:

Code: [SAM @ Chenwy SAM] $ GREP 'K ... D' DATA.F 47 OCT 3ZL1998 LPSX 43.00 KVM9D 512 483 May 5pa1998 USP 37.00 KVM9D 644 Make the above code slightly changed, the first two is uppercase letters, the middle two An arbitrary, and end with C:

Code: [SAM @ chenwy sam] $ grep '[AZ] .. 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 query a common query mode It is a date query. First query all records ending at 5 starts at 5 and 1 9 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, then 1 9 9, the remaining two numbers 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 must learn to use [] extract information. Assume that 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, using the following mode to be implemented.

Code: [SAM @ chenwy sam] $ grep '[0-9] [0-5 [0-6]' DATA.F 48 DEC 3BC1977 LPSX 68.00 LVX2A 138 483 SEPT 5ap1996 USP 65.00 LVX2C 189 47 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 Here to return a lot of information, if you want, you don't want it. Reference mode, the return result is correct, so this 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 can return a expected correct result. The following should pay attention to the difference between border characters 7, and the mode has the probability extraction contains all rows of the number 4 at least twice, the method is as follows:

Code: [SAM @ chenwy sam] $ grep '4 / {2, /}' data.f 483 May 5PA1998 USP 37.00 KVM9D 644 The above statement indicates that the number 4 is repeated twice, pay attention to the difference between bounded characters. Similarly, the extraction record contains the number 9 9 9 (three 9), the method is as follows:

Code: [SAM @ chenwy sam] $ grep '9 / {3, /}' DATA.F 219 DEC 2CC1999 CAD 23.00 PLV2C 68 If you want to query all the rows, the syntax is as follows, 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 47 OCT 3ZL1998 LPSX 43.00 KVM9D 512 219 DEC 2CC1999 CAD 23.00 PLV2C 68 484 NOV 7PL1996 CAD 49.00 PLV2C 234 Sometimes the number of repetitions is within a certain range, such as numbers or letters repeated 2 to 6 times, next example matching Digital 8 repeated 2 to 6 times, ending with 3:

Code: [SAM @ chenwy sam] $ cat myfile 83 888883 8884 88883 [SAM @ chenwy sam] $ grep '8 / {2,6/} 3' myfile 888883 888838, using grep matching "or" or "mode GREP Command plus - E parameter, this extension allows the use of extended mode to match. For example, to extract urban code 2 1 9 or 2 1 6, the method is as follows: 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 combination use ^ and $ can check the space line. Use the-c parameter to display the total number of credits:

Code: [SAM @ chenwy sam] $ grep -c '^ $' myfile Using - n parameter display does actually do:

Code: [SAM @ chenwy sam] $ grep -c '^ $' myfile10, matching special characters Query with special meaning characters, such as $. '"* [] ^ | / ?, Must be added before /. Suppose you want to query all rows that contain ".", The script is as follows:

Code: [SAM @ chenwy sam] $ grep '/.' Myfile or a double quotation number:

Code: [SAM @ chenwy sam] $ grep '/ "' myfile in the same way, if you want to query the file name 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, query formatting file name Use regular expressions to match any file name. The text file has its standard naming format in the system. Generally, up to six lowercase characters, followed by the sentence, then two uppercase characters.

Code: [SAM @ chenwy sam] $ grep '^ [AZ] / {1,6 /} /. [AZ] / {1, 2 /}' filename This Write method I don't know if there is any wrong 12 query IP address to view Nnn. NNN network address, if you have forgotten the rest of the second part, only two periods, such as nnn. nn.. To extract all NNN.NNN IP addresses, use [0 - 9] / {3 /} /. [0-0 / {3 /} /. The meaning is that any number appears 3 times, then follow the sentence, then the arbitrary number appears 3 times, then follow the sentence.

Code: [0-9] / {3 /} /. [0-9] / {3 /} /. '1, class name G R e p allows the use of the identical form of the international character pattern matching or matching mode. The regular expression class equivalent of the regular expression class equivalent of the class name and its equivalent

Quote: [[: Upper:]] [A - Z] [[: alNum:]] [0 - 9 A - ZA-Z] [[: Lower:]] [A - Z] [[: Space:]] Space or tab key [[: DIGIT:]] [0 - 9] [[: alpha:]] [A - Z A - Z] Example 1: Take 5 start, follow with at least two uppercase letters:

Code: $ grep '5 [[: Upper:]] [[: Upper]]' data.f Take all product code ending with P or D: Code: grep '[[: Upper:]] [[: Upper: ]]] [P, d] 'data.f2, using wildcard * matching mode

Code: $ cat testfile Looks Likes LOOKER LONG Try:

Code: grep "l. * S" testfile queries a single word, try the following mode:

Code: GREP "NG $" Testfile This will query all rows of the word NG in all files. 3, system GREP file passwd

Code: [root @ Linux_chenwy SAM] # GREP "SAM" / etc / passwd sam: x: 506: 4 :: / usr / sam: / bin / bash said above The above script query / etc / passwd file contains SAM string if Enter the following script:

Code: [root @ Linux_chenwy sam] # GREP "SAM" / etc / password grep: / etc / password: No file or directory will return G R e p command error code 'no such file or directory'. The above results indicate that the input file name does not exist, using the G R e P command - S switch, which can be mask the error message. Returns the command prompt without the error prompt 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 replaced:

Code: [root @ Linux_chenwy SAM] # GREP "SAM" / TEC / Password> / dev / null 2> & 1 Script meaning is the match command output or error (2> $ 1), and output the result to the system pool. Most system administrators said that / d e v / n u L L is a bit pool, it doesn't matter, it can be seen as a no-hole, there is no, never fill. The above two examples are not good because the purpose here only want to know if the query is successful. To save the query results of the g r e p command, 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 sam: x: 506: 4: : / usr / sam: / bin / bash script redirects the output to the directory / TMP file passwd. OUT. Use the PS command to use the G R e P with the PS X command to query the process running on the system. The PS X command is intended to display all processes running on the system. To see if the D n S server is running (usually called N a m e d), the method is as follows:

Code: [root @ Linux_chenwy SAM] # ps ax | grep "named" 2897 PTS / 1 S 0:00 grep named output should also include this G R e P command, because the G r e p command creates a corresponding process, and PS X will find it. Use the - V option in the G R e p command to discard the G r e p process in the P s command. If PS X does not apply to the user system, it is replaced with PS-EF. Here, since I don't have a DNS service, there is only a GREP process. Using GREP G R e p for a string is not only applied to files, it can also be applied to strings. Use the E c H o string command to use the E c H o, then use the pipe input to the G R e P command. Code: [root @ Linux_chenwy sam] # str = "Mary Joe Peter Pauline" [root @ Linux_chenwy sam] # echo $ str | GREP "Mary" Mary Joe Peter Pauline Match Success Realization.

Code: [root @ Linux_chenwy sam] # Echo $ Str | Grep "Simon" because there is no matching string, there is no output result. 4, EGREP E G R E P represents E x P R E S I O N or Extended GREP, and it is appropriate. E G R EP Accepts all regular expressions, a significant feature of E G R e P is a saved string with a file, and then transmits it to E G R E P as a parameter, uses a - F switch. If you create a file called G R E P T R I N g S, and enter 4 8 4 and 4 7:

Code: [root @ Linux_chenwy sam] # vi grepstrings [root @ Linux_chenwy sam] # Cat Grepstrings 484 47

Code: [root @ Linux_chenwy SAM] # EGREP -F Grepstrings Data.f 47 OCT 3ZL1998 LPSX 43.00 KVM9D 512 484 NOV 7PL1996 CAD 49.00 PLV2C 234 The above script matching D A T a. F contains 4 8 4 or 4 7 all records. When matching a large number of modes, the - f switches are useful, and in a command line, these patterns are obviously extremely cumbersome. If you want to query the storage code 3 2 L or 2 C C, you can use (|) symbol, meaning that "|" symbols or all.

Code: [Root @ Linux_chenwy SAM] # Egrep '(3ZL | 2CC)' DATA.F 47 OCT 3ZL1998 LPSX 43.00 KVM9D 512 219 DEC 2CC1999 CAD 23.00 PLV2C 68 216 SEPT 3ZL1998 USP 86.00 KVM9E 234 can use any multi-vertical characters | ", For example, want to check if there is account louise, matty or pauline in the system, using the who command and output to EGREP.

Code: $ WHO | EGREP (Louise | Matty | Pauline) Also use the ^ symbol exclude string. If you want to view the user on the system, but does not include M A T t-y and p A u L i n e, the method is as follows:

Code: $ WHO | EGREP -V '^ (Matty | Pauline)' If you want to query a list of files, including S H U T D O W N, S H U T D O W N, R EB O O T and R EB O O T, which can be easily implemented using E G R e P. Code: $ EGREP '(SHUTDOWN | Reboot) (s)?' *

转载请注明原文地址:https://www.9cbs.com/read-88380.html

New Post(0)