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     - Linux shell script to adjust the Task Scheduler (Linux)

- OpenGL Superb Learning Notes - GLSL language foundation (Programming)

- Ceph distributed storage system is installed on a CentOS 7.1 (Server)

- Linux LVM space expansion Demo (Linux)

- Linux Nginx FastDFS integration module is installed Nginx and FastDFS (Server)

- Android graphic introduction NDK installation and simple jni demo implementation (Linux)

- Axel install plug under CentOS 5/6 acceleration yum downloads (Linux)

- Lazarus for Raspbian installation (Linux)

- Linux-du and df command (Linux)

- Use PDFBox parse PDF file (Linux)

- Oracle JDK installation under Ubuntu Linux (Linux)

- Understanding the type in C ++ bitset (Programming)

- Source install Python3.4 on CentOS (Linux)

- Ubuntu 15.10 under Python + Apache + CGI fully configured (Server)

- Two alert log ORA Errors (Database)

- Zabbix monitoring different versions of RAID installation and monitoring and MySQL master-slave monitor (Server)

- Ubuntu 12.04 64-bit installation Redmine + Git + ReviewBoard (Linux)

- Oracle creates split and splitstr functions (Database)

- Linux productivity tools and tips (Linux)

- Linux Getting Started Tutorial: How to set up a static MAC address on VMware ESXi virtual machine (Mobile)

  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 will not work as expected:
grep '' / 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:
{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.
- OpenSSL: implementation creates a private CA, certificate signing request Explanation (Server)
- Without Visual Studio .NET Windows application development (Programming)
- Java string concatenation techniques (StringBuilder tips) (Programming)
- Thinking in Java study notes - initialization and cleanup (Programming)
- CentOS7 set boot directly into the command line interface (Linux)
- Port Telnet command to detect the remote host is turned on (Linux)
- Impact test noatime Linux file access time (Linux)
- The sublime into IDE (Linux)
- Puppet centralized configuration management system (Server)
- Shilpa Nair interview experience sharing RedHat Linux package management (Linux)
- IPTABLES configuration steps under Linux (Linux)
- BusyBox making the file system (Linux)
- Redis master-slave replication switch (Database)
- MongoDB common optimization settings in Linux (Database)
- Using Python multithreaded mistakes summary (Programming)
- Python common data type summary (Programming)
- SSH without password (Linux)
- Linux environment SSH login password instead of using the RSA Certificate (Linux)
- To configure parameter configuration and software installation and uninstallation under Linux (Linux)
- Linux environment MySQL master-slave synchronization (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.