Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Summary of grep commands

Shulou Source: shulou.com Published: 2022-06-03 06:59:42 09月11日 Update

Use and summary of grep command

Grep is best at filtering content / matching content.

The commonly used parameters are as follows

-v: excludes lines that have already matched the content (the smallest unit is: lines)

-o: only show the content that is received, it doesn't have any effect, don't remember it

-A: on the current line that matches the content, how many pieces of content are displayed behind the After= >

-B: on the current line that matches the content, how many pieces of content Beforce= > the previous meaning are displayed to the front

-C: on the current line that matches the content, how many pieces of content Center= > middle meaning are displayed at the center of the current behavior?

-the extended parameter of E:egrep plus it can match multiple conditions at one time, but should be separated by vertical bars |

-n: the line number that displays the content

It is recommended to use double quotes when using grep to match content

Examples are as follows:

1. Match the word hadoop in the / etc/passwd file

[root@oldboyLinux ~] # grep "hadoop" / etc/passwdhadoop:x:500:500::/home/hadoop:/bin/bash

2. The use of the parameter-v excludes the content with ftp. Do not display all lines with the word ftp.

[root@oldboyLinux ~] # grep-v "ftp" / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin.tcpdump:x:72:72::/:/sbin/nologinoprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologinhadoop:x:500:500::/home/hadoop:/bin/bash

3. Use regular expressions to match

If you want to filter out all configuration files that are annotated and exclude all lines that begin with #

[root@oldboyLinux etc] # grep-v "^ #" / etc/udev/rules.d/70-persistent-net.rules

4. Match the content ending with what character

[root@oldboyLinux etc] # grep "nologin$" / etc/passwd

5. Grep can also display the use of the line number-n parameter of the file content.

[root@oldboyLinux etc] # grep-n. / etc/passwd1:root:x:0:0:root:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6:sync:x:5:0:sync:/sbin:/bin/sync7:shutdown : x:6:0:shutdown:/sbin:/sbin/shutdown8:halt:x:7:0:halt:/sbin:/sbin/halt9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin10:uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin11:operator:x:11:0:operator:/root:/sbin/nologin12:games:x:12:100:games:/usr/games:/sbin/nologin13:gopher:x : 13:30:gopher:/var/gopher:/sbin/nologin14:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin15:nobody:x:99:99:Nobody:/:/sbin/nologin16:dbus:x:81:81:System message bus:/:/sbin/nologin17:vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin18:abrt:x:173:173::/etc/abrt:/sbin/nologin19:haldaemon:x:68: 68:HAL daemon:/:/sbin/nologin20:ntp:x:38:38::/etc/ntp:/sbin/nologin21:saslauth:x:499:76: "Saslauthd user": / var/empty/saslauth:/sbin/nologin22:postfix:x:89:89::/var/spool/postfix:/sbin/nologin23:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin24:tcpdump:x:72:72::/:/sbin/nologin25:oprofile: X:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin26:hadoop:x:500:500::/home/hadoop:/bin/bash

Tags: Content parameters meaning file line number command minimum one-time up and down function unit multiple typeface character commonly used suggestion beginning quotation mark time condition Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Linux Redmi Apple