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     - MySQL Statistics (Database)

- Ubuntu 14.04 to install file editor KKEdit 0.1.5 version (Linux)

- Linux beginners to develop the seven habits (Linux)

- Linux kernel compilation, the configuration of the motor drive (Programming)

- The difference Docker save and export commands (Linux)

- File permissions under Linux (Linux)

- Java regular expressions examples (Programming)

- To install Spotify in Ubuntu / Mint (Linux)

- How to install the client sqlplus under linux (Database)

- Ubuntu System Log Configuration / var / log / messages (Linux)

- Linux how to prohibit the use of Ping command (Linux)

- Fedora 20 Installation and Configuration (Linux)

- Nmcli based network management command-line tool (Linux)

- Under CentOS yum install Nginx smooth switch mounted to Tengine (Server)

- S5PV210 development board for embedded development environment to build under Ubuntu (Linux)

- quotacheck command file can not be created aquota.user and aquota.group solutions (Linux)

- Source install Python3.4 on CentOS (Linux)

- C language keywords Comments (Programming)

- Introduction Linux namespace (Linux)

- JavaScript event handling Detailed (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:      
 
- Keepalived achieve high availability Nginx Reverse Proxy (Server)
- Python extension module Ganglia 3.1.x (Linux)
- PostgreSQL 9.3.5 database installation under Ubuntu Server 14.04 (Database)
- To install the latest version of Shotwell 0.18 under Ubuntu (Linux)
- Linux installation Jetty deployment under RedHat5 8 (Linux)
- Apache site default home page settings (Server)
- Ubuntu 12.04 configure NVIDIA CUDA 5.5 Record (Linux)
- SecureCRT use the configuration detailed tutorial (Linux)
- CentOS7 complete step to install Hadoop2.7 (Server)
- AppCode developed Mac OS X application or shared library experience summary (Programming)
- CentOS7 installation performance monitoring system (Server)
- Linux kernel panic (because glibc result) Repair (Linux)
- MySQL Server Time Synchronization Problem (Database)
- Linux disk and File System Concepts (Linux)
- Linux common commands: nslookup, ls md5sum, uname, history, etc. (Linux)
- Depth understanding of the use of Spring Redis (Programming)
- Java Virtual Machine class loading mechanism and bytecode execution engine (Programming)
- Solve ORA-01012: not logged on (Database)
- Nginx Proxy timeout Troubleshooting (Server)
- How to choose the first programming language based on the life you want (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.