Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ grep command Detailed and relevant examples     - Java memory area and memory overflow exception (Programming)

- Android Touch message passing mechanism analysis (Programming)

- How to set the default Fedora from the command line (Linux)

- Under CentOS 7 installation and deployment environment Ceph (Server)

- 5 steps to help you become a good Docker contributors (Linux)

- Using VMware vSphere Client Linux virtual machine installation CentOS6.4 system (Linux)

- SSH Filesystem use a secure connection for network file system (Linux)

- Ubuntu modify DNS restart loss problem (Linux)

- MySQL Study of --Percona Server 5.5 Upgrade 5.6 (Database)

- Source install Python3.4 on CentOS (Linux)

- CentOS6.x and Windows XP and Windows Server 2003 Open IPv6 related matters (Linux)

- Linux kernel log --dmesg (Linux)

- MySQL 5.7 and 5.6 group by differences (Database)

- Wireless LAN security solutions (Linux)

- CentOS 6.5 can not connect to the network under VMware (Linux)

- Linux Shell introduces (Linux)

- Analysis of common mistakes when compiling MySQL installation (Database)

- Use dump restore mode fast backup and recovery system FreeBSD (Linux)

- Java JDK has been able to compile without warning (Programming)

- CentOS7 installation configuration (Server)

 
         
  grep command Detailed and relevant examples
     
  Add Date : 2018-11-21      
         
         
         
  I. matching character

It matches any single character

[] Matches any character in the specified range

[^] Matches any character within the specified range to fly

[: Alpha:] alphabetic characters

[: Lower:] Lowercase alphabetic characters

[: Upper:] uppercase characters

[: Digit:] Digital

[: Alnum:] Alphanumeric characters

[: Space:] whitespace characters (to prohibit printing), such as a carriage return, line feed, vertical tabs, and page breaks

[: Punct:] Punctuation characters

[: Cntrl:] control characters (prohibit printing)

[: Print:] printable characters

Usually when using two brackets, concrete will be used in the following examples.

II. The number of matches

* Matches any character in front of a

. * Matches any character of any length (note greedy mode, such as grep "r. * T" / etc / passwd)

x \ {m, n \} previously specified character appears at least m and at most appear N times.

x \ {m, \} previously specified character appears at least m

x \ {0, n \} previously specified character appears at most N times

x \ {m \} match exactly m times

? Matches its preceding character 0 or 1 times

III. Anchor symbol

1. ^ anchor the line grep "^ r..t" / etc / passwd

2. The end of line anchor $ grep "h $" / etc / passwd

3 ^ $ anchor blank lines grep "^ $" / etc / passwd

4. \ < (\ B) anchoring the first word grep "\ < r..t" / etc / passwd

5. \> (\ B) anchoring the first word grep "r..t \>" / etc / passwd

Example (confusingly):

Comprising at least one blank character grep "[[: space:]] \ {1, \}" / etc / passwd

It contains at least one non-blank character grep "[^ [: space:]] \ {1, \}" / etc / passwd

Not a blank character grep -v "[^ [: space:]] \ {1, \}" / etc / passwd

6. \ (\) character packet grep "\ (l..e \). * \ 1r"

Example:

grep --color "l \ ([13] \): \ 1:. *: \ 1" / etc / inittab

IV. Options

-v results negated

-i Ignore case of letters

-o Only the matched string (the line does not show other content)

-E Supports extended regular expressions

-A N display rows to match the following n lines

-B N display to match the line above the line n

The following lines show n -C n on the display to match the line

Exercise:

1. Find System user1 user information. (Advance built user11, myuser1) (error-prone)

grep "user1" / etc / passwd line contains all of user1

grep "\ < user1 \>" / etc / passwd surface can, however, add the following users can not

useradd -c "user1's uncle" / etc / passwd -c is a comment

grep "^ \ < user1 \>" / etc / passwd successful match

2. Find the system back to the beginning of the user with the digital user information.

grep "^ user [0-9] \ {1, \} \>" / etc / passwd

3. Analysis of / etc / inittab file the following features two lines of text, the preparation can be found precisely two lines similar pattern,

Requirement figures for each row must be the same.

l1: 1: wait: /etc/rc.d/rc 1

l3: 3: wait: /etc/rc.d/rc 3

grep "l \ ([13] \): \ 1:. *:. * \ 1" inittab

Extension: Match all above features: grep "l \ ([0-9] \): \ 1:. *:. * \ 1" inittab

If more than 10, you need to add the minimum matching: grep "l \ ([0-9] \ {1, \} \): \ 1:. *:. * \ 1" inittab

4. Display / proc / meminfo file to begin with is not case-sensitive s line

grep "^ [sS]" / proc / meminfo

5. Display the / etc / passwd to nologin end of the line

grep "nologin $" / etc / passwd

6. The / etc / inittab beginning with # and followed by one or more blanks, then any connection with any non-blank character of line

grep "^ # [[: space:]] \ {1, \} [^ [: space:]]" / etc / inittab

7. The / etc / inittab contains two colons in the middle of a number row

grep ": [0-9]:" / etc / inittab

The display /boot/grub/grub.conf file to one or more blank characters line

grep ": [0-9]:" / etc / inittab

9. Display the / etc / inittab file a start with a number and begins with a number of figures at the end of the same row.

grep "^ \ ([0-9] \). * \ 1 $" / etc / inittab

10. Central Africa show blank rows / etc / inittab file

grep -v "^ $" / etc / inittab

11. Remove the currently associated IP address of the network interface (does not include 127.0.0.1)

ifconfig | grep "inet addr" | grep -v "127.0.0.1" | cut -d: -f2 | cut -d "" -f1

ifconfig | grep -A 1 "eth" | grep -o "addr: [. 0-9] \ {1, \}" | cut -d: -f2

V. extended regular expressions

Regular expression differences:

\ (\) Replace ()

\ {\ {}} Replace

+ Number, the matching of the amount of its previous character one or more times

| Or

Example:

Ifconfig matching results appear 1-255 number is an integer

ifconfig | grep --color -E "\ < ([1-9] | [1-9] [0-9] | 1 [1-9] [0-9] | 2 [0-5] [0- 5]) \> "
     
         
         
         
  More:      
 
- Automated Password Generator: Linux under a special password generator (Linux)
- Create and modify Oracle temporary table space (Database)
- Windows using SSH management Ubuntu (Linux)
- RabbitMQ user roles and access control (Linux)
- Deploy OpenStack Juno on Ubuntu 14.04 (Linux)
- Difference between TCP and UDP protocols (Linux)
- Nginx log cutting and MySQL script regular backup script (Server)
- Nginx configuration support f4v video format player (Server)
- Ubuntu install Wireshark (Linux)
- Common Linux system performance monitoring command (Linux)
- 30 minutes with your Quick Start MySQL Tutorial (Database)
- Use ARChon runtime environment to run Android apps on Ubuntu (Linux)
- Linux operation and maintenance of the actual file system, file links (Linux)
- namespace mechanism Linux kernel analysis (Linux)
- Linux performance optimization tools perf top (Linux)
- The ORA-01113 error is handled with BBED without archiving (Database)
- Linux Troubleshooting: How to save the status of the SSH session is closed (Linux)
- CentOS 6.5 start ActiveMQ being given to solve (Server)
- EXP-00091: Exporting questionable statistics Processing Method (Database)
- High-performance JavaScript loops and flow control (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.