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     - RedHat command line and graphical interface switching (Linux)

- How to Install Redis server on CentOS 7 (Server)

- SpringMVC garbage processing (Programming)

- Linux remote connectivity tools -OpenSSH (Linux)

- zBackup: A versatile tool to remove duplicate backup (Linux)

- Objective-C basic program structure (Programming)

- Linux Network Security: nmap port scanning software (Linux)

- Oracle Data Guard LOG_ARCHIVE_DEST_n optional attribute parameters (Database)

- Convert MySQL date string to a NULL value exception handling (Database)

- Linux vi command list (Linux)

- GDB remote connections RX Probe online debug program (Programming)

- MySQL Data Types (Database)

- Linux System Getting Started Learning: After starting in Ubuntu or Debian, enter the command line (Linux)

- Linux, modify / etc / profile file (Linux)

- Distributed Hadoop1.2.1 cluster installation (Server)

- Boost-- time and date - (1) timer library introduction (Programming)

- Oracle Linux 5.9 configuration Xmanager (Linux)

- Growth since Oracle set the table space (Database)

- JDK installation notes under CentOS (Linux)

- Linux Network Programming - raw socket programming (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:      
 
- Android Studio and Git Git configuration file status (Linux)
- Linux kernel programming parameter passing between modules and function calls (Programming)
- How to fix the Ubuntu Error script returned error exit status 1 (Linux)
- How to Install Node.js in CentOS 7 (Linux)
- Transfer files to Windows and Linux (Linux)
- Lua study notes under ubuntu (Linux)
- PostgreSQL 9.3.2 Json type of use (Database)
- 20 Top Linux commands (Linux)
- Linux kernel log --dmesg (Linux)
- Polymorphism of the C ++ compiler and run-time polymorphism (Programming)
- How to monitor network usage by nload in Linux (Linux)
- MySQL Parameter Tuning Best Practices (Database)
- Oracle 11g maintenance partitions (eight) - Renaming Partitions (Database)
- LVM mirrored logical volume to achieve (Linux)
- OpenWrt modify flash size (Linux)
- Oracle Database High-Risk Vulnerability Warning (Database)
- After you change the GRUB boot disk partition repair (Linux)
- Linux module mechanism of (Programming)
- The simple multi-threaded Python (Programming)
- Oracle 10g in the unique and index problems (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.