Before you learn the regular expression, be clear and keep it in mind:In Linux, wildcards are interpreted by the shell, while regular expressions are interpreted by commands, so don't confuse them. Remember!!!usually have three text Processing tools/commands: grep, sed, awk, all three of them can interpret the regular. Let me show you how to use the grep command. First, use the
!
Extended grep (grep-E or egrep ):The main benefit of using extended grep is the addition of an additional regular expression metacharacter set.
Print all rows that contain NW or ea. If you do not use egrep but grep, no results w
applicants for the university departmentCat passwd | grep ^s List of exchange student application account holdersCat passwd | grep \ ' ^b.503\ ' lists the various grades of the motor system ...grep \ ' ^.\ ' myfile.txt lists all lines beginning with a period~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1. grep Simple In
About grep Regular Expressions in Linux, grep Regular Expressions
Wildcard
* Any character can be repeated multiple times
Any character, repeat once
[] Represents a character
For example, [a, B, c] indicates any one of abc.
Wildcard is used to match the file name
Regular Expression
Regular expressions are used to match matching strings in the file.
Ls find cp does not support regular expressions
However,
, and he will be like this:[Email protected] ~]# grep-n ' go\{2,5\}g ' Regular_express.txt18:google are the best tools for search keyword.What if I want 2 o ' goooo....g? In addition to being gooo*g, it can also be:[Email protected] ~]# grep-n ' go\{2,\}g ' Regular_express.txt18:google are the best tools for search Keyword.19:goooooogle yes!Extended grep (
Jason Developer Technology $5,500
Raj Sysadmin Technology $7,000
Randy Manager Sales $6,000
2. Use option-EThe GREP-E option can be used to extend the option to a regular expression. If you use the option-E for the grep command, you should use | To divide multiple pattern to implement or operations. [Plain] view plaincopy
Grep-e ' pat
Grep parameter, grep
Parameters:
-? At the same time, the upper and lower lines of matching rows are displayed? Line. For example, grep-2 pattern filename simultaneously displays the upper and lower two rows of matching rows.-B, -- byte-offset: print the block number of the row before the matching row.-C, -- count: only the number of matched rows is p
Reference Links:Http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.htmlthe grep family 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 re metacharacters , and fgrep i
numbers:Grep '^ \. [0-9]' FILENAME
Escape Character '.'The following regular expression finds the IP address 192.168.1.254 and cannot obtain the expected results:Grep '1970. 168.1.254 '/etc/hosts
All three points must be escaped:Grep '2017 \. 192 \. 1 \. 100'/etc/hosts
The following example matches only one address:Egrep '[[: digit:] {1, 3 }\. [[: digit:] {1, 3 }\. [[: digit:] {1, 3 }\. [[: digit:] {1, 3} 'filename
The words Linux or Unix are matched in case-insensitive mode:Egrep-I '^ (linux |
grep (abbreviated from globally search a Regular Expression and Print)is a powerful text search tool that can search for text using specific pattern matching (including regular expressions) and output matching rows by default. The grep family of Unix includes grep, Egrep, and Fgrep. Similar command findstr under Window
' [0-9][0-9]* ' Regular_express.txt Qualifying Continuous RE character range {}We can use it. With the re character and * To configure 0 to infinitely multiple repeating bytes, then what if I want to limit the number of repetitions in a range of intervals?For example, I want to find out two to five o continuous string, how to do? This is the time to use the qualified character {}. But since the symbol {and} is of special significance in the shell, we have to use the character \ To make him lose
special character before the special character is removed
* Indicates repetition of 0 to infinity before a re (regular expression) character
[List] Indicates a search for a string containing a list
[N1-N2] Indicates a search for the specified string range, such as [0-9] [A-z] [a-z], and so on
[^list] represents the range of the reverse string, for example [0-9] for non-numeric characters, [A-z] for non-uppercase character ranges
\{n,m\} indicates finding N to m first re characters
\{n,\} repres
Detailed usage of the grep command, grep command
In linux/unix, The grep command is very useful. It works with pipelines (|) and is very powerful. It is used to search text files. If you want to find a string in several text files, you can use the 'grep' command to search for the specified string in the '
The grep command is used to find files whose content contains the specified template style. If the content of a file conforms to the specified template style, the default grep command displays the column containing the template style. If no file name is specified or the given file name is "-", The grep command reads data from the standard input device.S
We often use PS | grep xxx is queried for the existence of a process, but the grep command is returned as a result by default, so that the return value of the command is 0 regardless of whether there is a process for the query.To avoid this scenario, you can use grep's-v parameter, meaning select non-matching lines (select unmatched rows).such as querying whether the cron service is runningPS |
I. Introduction to GREP commands and regular expressions1, grep (Global search REgular expression and print out of the line), that is, global searching regular expression and print out the matching row, it is a powerful text Search tool in Linux system, it according to user-specified "mode ( pattern) "Filters the target text to show rows that are matched by the pattern;2. Regular expressions are patterns wr
Refer to the code found on the Internet, did not expect the difference is so big, there is a project to 50GB~70GB code, 260 keywords to do scanning, need a relatively fast solution.
[gzhy@nearby stat]$ wc -l 1
234033 1
[gzhy@nearby stat]$ perl 1.pl
cost 1 seconds
zjtel : 32606
[gzhy@nearby stat]$ perl 2.pl
cost 111 seconds
zjtel : 32606
1.pl
#!/usr/bin/perl
my $time=time();
open(file,"1");
while(
2.pl
#!/usr/bin/perl
$time=time();
$count=`
-uppercase character ranges\{n,m\} means finding N to m previous re characters\{n,\} represents more than N of the previous re characterEgrep Summary:+ represents repeating one or more of the previous re charactersExample: Egrep ' Go+d ' regular_express.txtExpression Search (God) (good) (Goood) ... And so on string, o+ stands for [more than one o]? Represents a repeat of 0 or one of the previous re charactersExample:
RM[[email protected] a]# lsC.txtThe above command will include the a.txt and b.txt deletion of the Linux string.14. Qualifying Whole Word matching option:-W[Email protected] a]# Grep-rn "Li" *A.txt:1:linuxB.txt:1:linuxC.txt:1:li[Email protected] a]# GREP-RNW "Li" *C.txt:1:liThe 15.-E parameter matches more than one keyword at a time[Email protected] a]# cat/etc/passwd|
. []: a single character, such as [a], a meets the requirements. [-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements. . : all the individual characters. *: There are characters, the length can be 0. 4.grep command uses a simple instance$ grep ' test ' d*Displays all rows that contain test in a file that begins with D.$ grep ' test ' AA bb ccDis
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.