Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ grep, egrep and regular expressions     - Polymorphism of the C ++ compiler and run-time polymorphism (Programming)

- Redhat 7 modify the default run level method --RHEL7 use systemd to create a symbolic link to the default runlevel (Linux)

- Python script running in the background (Programming)

- CentOS 6.4 compiler installed MySQL 5.6.14 (Database)

- Linux System Getting Started Tutorial: How to automatically set the JAVA_HOME environment variable on Linux (Linux)

- Linux System Tutorial: How to Check MariaDB server versions (Database)

- Objective-C basic program structure (Programming)

- Introduction to thread pooling and simple implementation (Programming)

- How to use the beta / unstable version of the software in Debian library (Linux)

- CentOS 6.5 installation VNCServer implement graphical access (Server)

- ogg start being given libnnz11.so: can not open shared object file (Database)

- Matters Oracle 11.2 single instance when connecting ASM need to pay attention and deal with the problem (Database)

- Cooling solutions Ubuntu system (Linux)

- Graphical development environment to build Android under Ubuntu 11.04 (Linux)

- Linux Getting Started Tutorial: / var / spool / clientmqueue fill the root directory (Linux)

- The top command under Linux (Linux)

- Linux kernel log --dmesg (Linux)

- Hadoop vs spark (Server)

- Do you know how to build the Linux kernel (Programming)

- Python programmers most often committed ten errors (Programming)

 
         
  grep, egrep and regular expressions
     
  Add Date : 2017-01-08      
         
         
         
  Linux, everything is a file: text files, search the text file is very critical and commonly used.

Linux text search tools: grep, egrep, fgrep, text search refers to search the text file content.

File name search is characteristic of the linux bash, globbing, file name wildcard, little windows in the search for similar, but with powerful features. Usually with ls command. Refer to the video: bash basic features (01)

globbing: Meta characters: *: wildcard for any length of any character? : Used to match any single character []: a ratio within a specified range of characters with, [^]: Anti character within the range for taking.

Summary: globbing wildcard file name: not only defines a length, and define the available range of characters.

Since you want to search, you must have a filter, here we must use regular expressions, he is a linux text-processing tools are very basic and important.

Regular expressions (filter) regular expression metacharacters to normal character combination.

Basic regular expression: grep

Extended regular expressions: egrep, grep E,

                fgrep: fast, does not support the use of regular expressions

grep: Global search Regular expression and print out the line

grep command:

    grep [OPTION] PATTEN [FILE ...]

        --color = auto

        -o: Show only matched filter pattern to content

        -i: search is not case-sensitive character grep "no"

        -v: Display can not be matched to the filter pattern lines

Here used a parameter of --color grep, color there are three values to choose from: never, always, auto. And auto difference always is, always will be, in any case give added color-coded to match the field, when through a pipe or redirection will be more of some control characters, the result will become
export ^ [[1; 32m ^ [[KGREP ^ [[m ^ [[K_OPTIONS = '- color = always'
export ^ [[1; 32m ^ [[KGREP ^ [[m ^ [[K_COLOR = '1; 32'
The auto output only when coupled to the terminal colors.
You can add in your .bashrc
export GREP_OPTIONS = '- color = auto'
Highlight matching to achieve, what specific color, you can
export GREP_COLOR = 'a; b' # default is 1; 31, that is highlighted in red
To set up, in which:
a: You can choose: [0,1,4,5,7,8] b: choose:

Close all the properties of 30 black 0
1 set high brightness 31 red

4 Underline 32 green

5 33 yellow flashing

7 Anti-significant 34 blue

8 Blanking 35 purple

36 cyan

37 white

Basic regular expression metacharacters:

Character matches:

      : Match any single character

      []: With any single character within the specified range

      [^]: Matches outside the specified range of any single character, equivalent to [] negated

          [0-9], [[: digit:]], [^ 0-9] [^ [: digit:]]

            [A-z], [[: lower:]]

            [A-Z], [[: upper:]]

            [[: Space:]] spaces

          [[: Punct:]] punctuation

          [0-9a-z A-Z], [[: alnum:]]

            [A-zA-Z], [[: alpha:]]

Match times: providing a control character behind the desired matching characters used to express the number of characters in front of the specified match

      *: Used to match the left side of the character of any length, it represents 0, 1 or more times

        "Ab * c" = ac, or, abbc, or abbbc, or abbbbc

      *: Any character of any length, default greedy mode to work,

    \ ?: Match their characters before the match 0 or 1, showing the character to the left dispensable

        "Ab \? C" = ac, or abc

      \ +: Matching front (left) characters one or more times

        "Ab \ + c" = abc, abbc, abbbc

      \ {M \}: Exact match left side of the character m times. ab \ {2 \} c = abbc

      \ {M, n \}: at least m and at most n times. ab \ {0,3 \} c = ac, abc, abbc, abbbc

      \ {0,3 \}: up to 3 times

    \ {M, \}: minimum m times

Character anchor position "matching position is defined:

      ^: Anchor line. ^ Ab

      $: The anchor end of the line. ab $

      ". ^ Ab * ba $": In the middle of any of the characters to begin with ab ab end

      "^ $": Match a blank line, grep "^ $" /etc/rc.d | wc l statistics showing the number of blank lines

      "^ PATTERN $: to match the whole line." ^ A4 * $ "

      Anchor word: continuous string of non-special characters

        \ <: Anchoring the first word, but also \ b # grep "\
        \>: Anchor ending, but also \ b # grep "ab \>" / etc

        \ : matches PATTERN matches to whole words

      # Ifconfig | grep "\ < [0-9] \ {2 \} \ >": Exact Match 2 digital word

Grouping: \ (\)

    Grouping parentheses in the pattern to match the content of the regular expression engine will be recorded in the implementation process down and save the built-in variables; these variables are \ 1, \ 2, ...

\ 1: from the left, first left parenthesis and a right parenthesis intermediate pattern paired matched to the content; \ 2 and so on.

# "\ (Ab \) * c"

Extended regular expression metacharacters:

Character matches: Same basic regular expressions

Match times:

*: 0 or more times

?: 0 or 1;

+: 1 or more;

{M}: Exact Match m times;

{M, n}: at least m and at most n times;

Anchored:

^: Anchoring the line

$: End of line anchor

\ <, \ B: word-initial anchor; to the left represents the word mode;

\>, \ B: ending anchor; for the right of representing a word patterns;

Grouping:

With basic regular expressions

()

Or support concept

a | b: matching a or b

Exercise:

1, the display / etc / passwd file to bash the end of the line

# Grep "bash $" / etc / passwd

# Grep -E "bash $" / etc / passwd

# Egrep "bash $" / etc / passwd

2, a / etc / passwd file in the two-digit or three-digit

# Grep "\ < [[: digit:]] \ {2,3 \} \ >" / etc / passwd

# Egrep "\ < [0-9] {2,3} \ >" / etc / passwd

3, showing the results of the command `netstat -tan` to 'LISTEN' followed by zero, one or more blank characters at the end of the line

# Netstat -tan | grep "LISTEN [[: space:]] * $"

  # Netstat -tan | egrep "LISTEN [[: space:]] * $"

4, add users bash, testbash, basher and nologin user (user shell nologin to / sbin / nologin); then find the / etc / passwd file in the same row as the user name and its shell name

# Grep "^ \ ([[: alnum:]] \ + \> \). * \ 1 $" / etc / passwd

# Egrep "^ ([[: alnum:]] + \>). * \ 1 $" / etc / passwd

5, the display on the current system root, the default shell CentOS or user1 user and UID (create these users in advance, if it exists)

# Grep -E "^ (root | centos | user1) \>" / etc / passwd | cut -d: -f3,7

# Egrep "^ (root | centos | user1) \>" / etc / passwd | cut -d: -f3,7

6, to find /etc/rc.d/init.d/functions file a word (word intermediate can exist underscore) followed by a set of parentheses row

# Egrep -o "\ <[[[: alnum:] _] + \> \ (\)" / etc / rc.d / init.d / functions

7, echo out a path, and then egrep find out the path to the base name; further use egrep remove its directory name


# Echo / etc / sysconfig / network-scripts / | egrep -o "? [[: Alnum:] -] + / $"

# Echo / etc / sysconfig / network-scripts | egrep -o "(/.*/)"

8 to find out the results of digital ifconfig command 1-255

# Ifconfig | egrep "\ < ([1-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0-4] [0-9] | 25 [0-5]) \ > "
     
         
         
         
  More:      
 
- Smooth upgrade to OpenSSH 6.7 Procedure (Linux)
- MySQL use benchmarking tool sysbench (Database)
- Linux user opens a number of adjustment processes (Linux)
- Ubuntu 15.10 / 14.04 install subtitling software Aegisub (Linux)
- How common Linux automation tasks (Server)
- RabbitMQ tutorial examples: RabbitMQ installation under Windows (Linux)
- How to troubleshoot Windows and Ubuntu dual system time is not synchronized (Linux)
- C # function (Programming)
- Why learn and use C language (Programming)
- SUSE Linux firewall configuration notes (Linux)
- Linux5.8 installed phpMyAdmin was unable to issue related php-mcrypt (Database)
- To compile and install OpenCV-2.3.1 FFmpeg-2.1.2 under CentOS (Linux)
- Cacti Linux-based system monitoring and alarm (Linux)
- Oracle row and column switch to turn columns (Database)
- Python data types summary (Programming)
- Slice MyCAT common fragmentation rules of enumeration (Database)
- Nine tips to protect the security of Linux desktop (Linux)
- The PostgreSQL database pg_dump command line does not enter a password method (Database)
- Let Linux boot methods to enter characters interface and set FrameBuffer resolution methods (Linux)
- Installation and use the telnet command under CentOS 6.x (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.