Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Three minutes to teach you to easily grasp the grep command regular expression     - Use Docker containers (Linux)

- See Linux kernel, CPU, memory, and various versions of the command module and means (Linux)

- Autojump: an advanced cd command in the Linux file system fast navigation (Linux)

- MySQL binary packages install for RedHat Linux Enterprise 6.4 (Database)

- Generate two-dimensional code parsing code Java (Programming)

- Linux / UNIX: Use the dd command to create a 1GB size binary (Linux)

- CentOS7 set boot directly into the command line interface (Linux)

- When Vim create Python scripts, vim autocomplete interpreter and encoding method (Programming)

- Linux System Getting Started Learning: Change the name of the network interface on CentOS7 (Linux)

- How to use jgit to manage Git submodule (Linux)

- To compile and install MySQL 5.7.7 RC under CentOS 7.1 (Database)

- The sublime into IDE (Linux)

- What factors affect the performance of Java calls (Programming)

- To learn linux security (Linux)

- Docker deployment practices in Ubuntu (Server)

- Let the terminal under Mac OS X as like Linux has displayed a variety of colors (Linux)

- Git uses a standard process (Linux)

- Implement binary search algorithm in C language (Programming)

- Alien Magic: RPM and DEB Mutual Convert (Linux)

- Comparison of C # and Java (Programming)

 
         
  Three minutes to teach you to easily grasp the grep command regular expression
     
  Add Date : 2018-11-21      
         
         
         
  How to use regular expressions with the grep command in Linux systems and Unix-like operating system as well?

Linux system comes with a version of GNU grep tool supports expanding the regular expression. All Linux systems are installed by default GNU version grep. grep command is used to retrieve text information anywhere on a single server or workstation.

First, a quick overview of regex

1, how to match you're looking for?

Regular expressions just each input line to match the pattern. Pattern is a sequence of characters. The following are examples:

For example: "^ w1", "w1 | w2", "[^]".


Retrieval 'vivek' in '/ etc / passswd' in.
grep vivek / etc / passwd

Output results of the case:
vivek: x: 1000: 1000: Vivek Gite ,,,: / home / vivek: / bin / bash
vivekgite: x: 1001: 1001 :: / home / vivekgite: / bin / sh
gitevivek: x: 1002: 1002 :: / home / gitevivek: / bin / sh

In any case, the search 'vivek' (that is not case-sensitive):
grep -i -w vivek / etc / passwd

Case-insensitive retrieve 'vivek' and 'raj':
grep -E -i -w 'vivek | raj' / etc / passwd

In the last example, the use of the extended regular expression pattern.

Retrieve the contents of the fixed location:
 You can use ^ and $ symbols were forced to a regular expression match the beginning or end of a line of. The following example shows to 'vivek' the beginning of the text.
grep ^ vivek / etc / passwd

Example output:
vivek: x: 1000: 1000: Vivek Gite ,,,: / home / vivek: / bin / bash
vivekgite: x: 1001: 1001 :: / home / vivekgite: / bin / sh

You can display only text line with vivek beginning. For example does not show is the beginning of a word such vivekgite, vivekg of.
grep -w ^ vivek / etc / passwd

Retrieving with 'foo' at the end of the text:
grep 'foo $' FILENAME

You can also search for blank lines in such a way the following:
grep '^ $' FILENAME

2, how to match the specific character?

Match 'Vivek' or 'vivek':
grep '[vV] ivek' FILENAME

Or you can:
grep '[vV] [iI] [Vv] [Ee] [kK]' FILENAME

You can match numbers (such as matching vivek1 or Vivek2):
grep -w '[vV] ivek [0-9]' FILENAME

You can match two digits (for example matching foo11, foo12):
grep 'foo [0-9] [0-9]' FILENAME

Not just numbers, you can match the letters:
grep '[A-Za-z]' FILENAME

Show all contain "w" or "n" letters line of text:
grep [wn] FILENAME

In the expression in parentheses, in the "[:" and ":]": the list of representatives belonging to the class of all characters in character class name appended. Standard character class names:
[: Alnum:] - alphanumeric characters.
[: Alpha:] - Alphabetical
[: Blank:] - spaces and tabs.
[: Digit:] - Digital: '0123456789'.
[: Lower:] - lowercase letters: 'a b c d e f'.
[: Space:] - Special characters: tab, newline, vertical tab, form feed, carriage return, and space.
◾ [: upper:] - capital letters: 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.

In the following example, matches all capital letters:
grep '[: upper:]' FILENAME

3. How to use wildcard characters?

You can use "." Instead of a single character. In the following example, the query all the words with the letter "b" at the beginning of the letter "t" at the end of three characters.
grep '\ ' FILENAME

In the above example,
◾ \ ◾ \> at the end of a word matches a string of spaces

Retrieve and print all the letters of the two results:
grep '^ .. $' FILENAME

And to retrieve and display all the results of the digital beginning '':
grep '^ \. [0-9]' FILENAME

Escape character '.'
The following regular expression search results IP address 192.168.1.254 will not work as expected:
grep '192.168.1.254' / etc / hosts

Three points need to be escaped:
grep '192 \ .168 \ .1 \ .254' / etc / hosts

The following example will match only one address:
egrep '[[: digit:]] {1,3} \ [[: digit:]]. {1,3} \ [[: digit:]]. {1,3} \ [[:. digit:] ] {1,3} 'FILENAME

Below the lower case and matching words Linux or Unix:
egrep -i '^ (linux | unix)' FILENAME

Second, in-depth exploration grep Advanced Search mode


1, how to retrieve a having a '-' at the beginning of the pattern?

Use the -e option to search for all occurrences of '-test-' results. grep will try to '-test-' as an option to resolve:
grep -e '--test--' FILENAME

2, how to use the OR logic operation in the grep?
grep -E 'word1 | word2' FILENAME
### OR ###
egrep 'word1 | word2' FILENAME

Or may do so
grep 'word1 \ | word2' FILENAME

3, how to use the AND logic operation in the grep?

Show all results containing the word 'word1' and 'word2' according to the following syntax:
grep 'word1' FILENAME | grep 'word2'

Or you can:
grep 'foo * bar \ |.. word3 * word4' FILENAME

4, how to test sequence?

You can use the following syntax to test a number of repetitions in the sequence of characters:
{N}
{N,}
{Min, max}

Results matching string includes two letters of v:
egrep "v {2}" FILENAME

In the following example will retrieve the file contains "col" and "cool" string of results:
egrep 'co {1,2} l' FILENAME

The following example will contain the results of matches at least three letters of c:
egrep 'c {3,}' FILENAME

The following example will match "91-1234567890" format phone number (ie, "two digits - ten digits")
grep "[[: digit:]] \ {2 \} [-] \ [[: digit:]]? \ {10 \}" FILENAME

5, how to grep the output highlight label?

Use the following example syntax:
grep --color regex FILENAME

6, how to make the output of grep to show only matching part and not the whole line?

Use the following example syntax:
grep -o regex FILENAME

Third, regular expression operators summary


Regular Expressions
Operator Meaning
. Matches any single character.
? Matches the preceding character 0 or 1 times.
* Matches the preceding character ≥0 times.
+ Matches the preceding character ≥1 times.
{N} N times before the match a character.
{N,} Match the previous character ≥m times.
{N, M} Match the character N to M times.
- If the list of a list or a range of end point in showing this range.
^ Start tag, indicating the start position to match an empty string. Also represents a character not within the scope of the list.
$ End tag. Matches an empty string.
\ B word lock symbol. Location at the edge of a word matches the empty string.
\ B non-edge position in a word matches the empty string.
\ \> Matches the empty string at the end of a word.
Fourth, on the grep and egrep

egrep That grep -E, it is the pattern as an extended regular expression interpreter. grep help documentation in this definition:

 In basic regular expressions the meta-characters, +, {, |, (, and) lose their special meaning;? Instead use the backslashed versions \ ?, \ +, \ {,
       \ |, \ (, And \).
       Traditional egrep did not support the {meta-character, and some egrep implementations support \ {instead, so portable scripts should avoid {in
       grep -E patterns and should use [{] to match a literal {.
       GNU grep -E attempts to support traditional usage by assuming that {is not special if it would be the start of an invalid interval specification.
       For example, the command grep -E '{1' searches for the two-character string {1 instead of reporting a syntax error in the regular expression.
       POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
     
         
         
         
  More:      
 
- Linux System Getting Started Learning: modify environment variables in Linux PATH (Linux)
- Safe learning through self-extracting package to achieve invasion (Linux)
- Hibernate + JUnit test entity class generate database table (Programming)
- Using Vagrant to build multi-platform environment (Server)
- A process of how to get the current traffic in GNU Linux (Linux)
- CentOS 6.5 platform offline compile and install PHP5.6.6 (Server)
- The callback function used in C ++ (Programming)
- ASP.NET 5 (vNext) Linux deployment (Server)
- CentOS system Amoeba + MySQL Master-slave configuration (Database)
- MySQL time field based partitioning scheme summary (Database)
- How to install the client sqlplus under linux (Database)
- Analysis of Java keyword final (Programming)
- Installation image conversion tool on your Ubuntu Converseen 0.8.1 (Linux)
- mysql_config_editor encryption and decryption of the new features of MySQL realization (Database)
- Use a soft Raid play multiple SSD hard drive performance and enhance data security (Linux)
- PHP interview questions of design patterns (Programming)
- Linux five security TIPS (Linux)
- Zabbix Agent (Server)
- How to statistical data of various size Redis (Database)
- awk Programming Model (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.