Use of grep in Linux CentOS 7

Source: Internet
Author: User
Tags egrep

9.1 Regular Introduction _grep (1) What is a regular
Regular is a string of regular strings. Mastering a good regular is a great help for the shell. Every language has a regular, the principle is the same. The main commands are: Grep/egrep sed awk.
(2) grep
grep  filters The specified keyword grep [-cinvrabc]  ' word '  filenamegrep -c  displays the total number of lines that contain keywords Grep -n   Show line numbers that contain keywords grep -i  case-insensitive grep -v  grep -r  traverse all subdirectories grep -a  back root number , filter out the line that meets the requirements and the following n rows grep -b  the root number, filter out the rows that meet the requirements, and the number of n rows above grep -c , filter out the rows that meet the requirements, and the next n rows 
[[email protected] ~]# grep -c root /etc/passwd2[[email protected] ~]# [[email protected] ~]# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/ Bash10:operator:x:11:0:operator:/root:/sbin/nologin[[email protected] ~]#[[email protected]  ~]# grep -vn root /etc/passwd2: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/NOLOGIN11:GAMES:X:12:100:GAMES:/USR/GAMES:/SBIN/NOLOGIN12:FTP:X:14:50: ftp user:/var/ftp:/sbin/nologin13:nobody:x:99:99:nobody:/:/sbin/nologin[[email protected] tmp]#  grep -i root 1.txt 1root:x:0:0:root:/root:/bin/bash1root:x:0:0:root:/root:/bin/ Bashoperator:x:11:0:operator:/root:/sbin/nologin[[email protected] tmp]#[[email protected] ~]# grep -r  root /tmp//tmp/history_bak.log:      1 ./root/shell/mail.sh 111  111/tmp/history_bak.log:      1 /bin/grep root /etc/passwd/ Tmp/history_bak.log:      2 /usr/local/mysql/bin/mysql -u root  -p/tmp/history_bak.log:      1 /usr/local/mysql/bin/mysqldump -u  root -p /tmp/history_bak.log:      1 /usr/local/mysql/bin/ mysqldump -u root -p password  "qing123." /tmp/history_bak.log:      1 awk  "/root/"  1.txt/tmp/history_ bak.log:      1 awk  "/root|fxq/"  1.txt/tmp/history_bak.log:       1 awk /root|fxq/ 1.txt/tmp/history_bak.log:      1 cat /etc/passwd | grep  Root/tmp/2.txt:1root:x:0:0:root:/root:/bin/bash/tmp/2.txt:operator:x:11:0:operator:/root:/sbin/nologin/tmp/1. txt:1root:x:0:0:root:/root:/bin/bash/tmp/1.txt:operator:x:11:0:operator:/root:/sbin/nologin[[email  Protected] ~]# grep -na2 root /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/nologin--10:operator:x:11:0:operator:/root:/sbin/ nologin11-games:x:12:100:games:/usr/games:/sbin/nologin12-ftp:x:14:50:ftp user:/var/ftp:/sbin/nologin[[ Email protected] ~]# [[email protected] tmp]# grep -nb2 root /etc /passwd1:root:x:0:0:root:/root:/bin/bash--8-halt:x:7:0:halt:/sbin:/sbin/halt9-mail:x:8:12:mail:/var/spool/mail :/sbin/nologin10:operator:x:11:0:operator:/root:/sbin/nologin[[email protected] tmp]# [[email  protected] tmp]# grep -nc2 root /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/nologin--8-halt:x:7:0:halt:/sbin:/sbin/halt9-mail:x:8:12:mail:/var/ spool/mail:/sbin/nologin10:operator:x:11:0:operator:/root:/sbin/nologin11-games:x:12:100:games:/usr/games:/ sbin/nologin12-ftp:x:14:50:ftp user:/var/ftp:/sbin/nologin[[email protected] tmp]#
9.2 grep in
Grep-n ' root '/etc/passwdgrep-nv ' root '/etc/passwdgrep ' [0-9] '/etc/inittab filter lines with numbers grep-v ' [0-9] '/etc/inittab filter not Contains digitized lines grep-v ' ^# '/etc/inittab filter with non-# start line grep ' ^[^a-za-z] ' test.txt filter lines starting with non-letters
[[email protected] tmp]# grep  ' [0-9] '  /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x : 1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7 : lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/ shutdownhalt:x:7:0:halt:/sbin:/sbin/halt[[email protected] tmp]# grep -vn  ' [0-9] '  /etc/inittab 1:# inittab is no longer used when using  Systemd.2: #3:# adding configuration here will have no effect on  Your system.4: #5: # ctrl-alt-delete is handled by /usr/lib/systemd/system/ Ctrl-alt-del.target6: #7:# systemd uses  ' targets '  instead of runlevels. by  default, there are two main targets: 8:# 11:# 12:# To  View current default target, run: 13:# systemctl get-default 14:# 15:# to set a  default target, run: 16:# systemctl set-default target.target 17:# [[ email protected] tmp]#  [[email protected] tmp]# grep -nv  ' ^# '  1.txt 4:33335:11116:1root:x:0:0:root:/root:/bin/bash 7:1root:x:0:0:root:/root:/bin/bash 8 : Bin:x:1:1:bin:/bin:/sbin/nologin
9.3 grep under
grep ' R.O ' test.txt. Match any character grep ' oo* ' test.txt * match * preceding character any time (>=0) grep '. * ' test.txt. * Match any arbitrary character grep ' o\{2\} '/etc /PASSWD grep-e ' o{2} '/etc/passwdegrep ' o{2} '/etc/passwdegrep ' o+ '/etc/passwd + match + 1 or more times preceding (>=1) egrep ' oo? '/etc/p Asswd? match? Front character 0 or 1 times (0 or 1) egrep ' Root|nologin '/etc/passwd match root or Nologinegrep ' (oo) {2} '/etc/passwd range
[[email protected] tmp]# egrep ' o{2} '/etc/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/ Nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x : 89:89::/var/spool/postfix:/sbin/nologinmailnull:x:47:47::/var/spool/mqueue:/sbin/nologinsmmsp:x:51:51::/var/ Spool/mqueue:/sbin/nologin[[email protected] tmp]#
Extended

Filter all the lines in the *.php document that contain eval in a directory

grep -r --include= "*.php"   ' eval '  /data/
[[email protected] fxq]# grep -r --include= ' *.php '   ' eval '  /  / Backup/hszd_zabbix/bak/var_www_html_zabbix/jsloader.php:header (' cache-control: public,  Must-revalidate ');/backup/hszd_zabbix/bak/var_www_html_zabbix/include/triggers.inc.php: * substitute  macros in the expression with the given values and evaluate  its result./backup/hszd_zabbix/bak/var_www_html_zabbix/include/triggers.inc.php:function  Evalexpressiondata ($expression,  $replaceFunctionMacros)  {/backup/hszd_zabbix/bak/var_www_html_zabbix /include/triggers.inc.php:eval (' $result  =  trim ($evStr). '); backup/hszd_zabbix/bak/var_www_html_zabbix/include/schema.inc.php: ' Evaltype '  => array (/backup/ hszd_zabbix/bak/var_www_html_zabbix/include/schema.inc.php: ' Evaltype '  => array (/backup/hszd_ zabbix/bak/var_www_html_zabbix/include/schema.inc.php: ' Evaltype '  =>  array ( 


This article is from the "Feng Xiaoqing blog" blog, make sure to keep this source http://fengyunshan911.blog.51cto.com/995251/1968446

Use of grep in Linux CentOS 7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.