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     - Ubuntu install virtual machine software VirtualBox 4.3.10 (Linux)

- Atlassian Linux Shell Scripting the confluence remote backup methods (Linux)

- Teach you how to synchronize Microsoft OneDrive in Linux (Linux)

- Git / Github use notes (Linux)

- Install mono offline on CentOS (Server)

- Mount and unloading disks under Linux (Linux)

- Shell generated using automated configuration script Orabbix (Database)

- How to use SVN to manage our source code (Server)

- JBoss7 configuration - Supports IPv4 and IPv6 dual-stack environment (Server)

- Django how to generate content in non-HTML formats (Programming)

- Error: Unable to establish connection with FTP SFTP server (Server)

- cursor_sharing induced error ORA-00600 (Database)

- Programmers Do not neglect debugging techniques (Programming)

- Apache POI Excel Document Processing (Linux)

- Java objects are taking up much space (Programming)

- CentOS7 Kubernetes used on container management (Server)

- Inxi: Get Linux system and hardware information (Linux)

- Ubuntu 12.04 installed OpenCV 2.3.1, binary image (Linux)

- MySQL 5.7.10 source code for the latest version of the installation process in detail (Database)

- Linux upgrade GCC 4.8.1 clear and concise tutorials (Ubuntu 12.04 64-bit version as an example) (Linux)

 
         
  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:      
 
- Under Ubuntu on how to use iptables firewall (Linux)
- ORA-01157 & ORA-01110 Troubleshooting (Database)
- Samba public folder permissions (Server)
- C ++ containers (Programming)
- Subsequent binary search tree traversal sequence (Programming)
- CentOS7 install and configure Nagios (Server)
- To install Scribus 1.4.4 under ubuntu (Linux)
- Install Xshell on Mac OS X (Linux)
- Linux modify the network interface name (Linux)
- Fedora 22 how to play rmvb mp4 mkv video files (Linux)
- Availability Hadoop platform - Oozie Workflow (Server)
- CentOS 6.6 command-line automatic completion (Linux)
- How to Install Apache on Linux servers (Server)
- To install Spotify in Ubuntu / Mint (Linux)
- The Samba service does not have permission to access (Server)
- JDK comes with tools JPS (Linux)
- Simple configuration shell scripts virtual machine environment experiment (Linux)
- Use a soft Raid play multiple SSD hard drive performance and enhance data security (Linux)
- ethtool command Detailed (Linux)
- CoreOS quick installation to get started on a PC (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.