Linux command grep
The grep command is used to search for text, or to search from a given file for a file containing a given string or word within a row. Typically, grep displays matching rows. Use grep to search for lines of text that include one or more regular expressions to match, and then show only the rows that match.
The syntax of the grep command:
grep ' word ' file name
grep ' word ' file 1 file 2 File 3
grep ' string 1 string 2 ' file name
Commad | grep ' Something '
Commad Option 1 | grep ' data '
grep--color ' data ' file name
Use grep to search for a file
Search for files
To search for Linux users under the/etc/passwd file, enter:
[[email protected] ~]# grep linux/etc/passwd
Output content:
Linux:x:500:500::/home/linux:/bin/bash
You can use grep to force ignoring the case. For example, use the-I option to match Linux Linux, Linux, and other combinations:
Grep-i linux/etc/passwd
Recursion using grep
You can use grep recursively to search. For example, search all files containing the string "192.168.57.9" in the file directory
[Email protected] ~]# grep-r "192.168.57.9"/etc/
Or
[Email protected] ~]# grep-r "192.168.57.9"/etc/
Output results
/etc/sysconfig/network-scripts/ifcfg-eth0:ipaddr=192.168.57.9
You will see that the results of the search to 192.168.57.9 each row are prefixed to find a matching file name. The file name contained in the output can be added with the-H option to suppress the output:
Such as:
[Email protected] ~]# grep-hr "192.168.57.9"/etc/
ipaddr=192.168.57.9
Or
[Email protected] ~]# grep-h-R "192.168.57.9"/etc/
ipaddr=192.168.57.9
Use grep to search for text
When you search for Linux, the grep command will match linux,linux123,linux35 and other strings that contain Linux, and you can use the-w option to force output only those lines that contain only that whole word.
[Email protected] ~]# grep-w linux/etc/passwd
Search two different words using the grep command
The Egrep command is as follows:
Egrep-w ' Word1 | Word2 ', Path/to/file
The regular expression is used here, so the Egrep command, the extended grep command, is used.
Number of rows matched by statistical text
The grep command can display the number of matches in each file by adding the-C parameter:
Grep-c ' word '/path/to/file
The Pass-N option allows you to output the line number of the line to which the match is to be added:
Grep-n ' Root '/etc/passwd
Such as:
[[email protected] ~]# grep-n ' root '/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
Invert match
Use the-V option to output content that does not contain a match, and the output contains only those lines that do not contain a given word, such as outputting all rows without root
[[email protected] ~]# grep-n ' root '/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep-n "root"/etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep-v "root"/etc/passwd
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
Nobody:x:99:99:nobody:/:/sbin/nologin
Vcsa:x:69:69:virtual Console Memory Owner:/dev:/sbin/nologin
saslauth:x:499:76: "SASLAUTHD user":/var/empty/saslauth:/sbin/nologin
Postfix:x:89:89::/var/spool/postfix:/sbin/nologin
Sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologin
Linux:x:500:500::/home/linux:/bin/bash
Piping and grep commands
grep is often used with pipelines
If the name of the disk device is displayed:
[Email protected] ~]# DMESG | Egrep ' (s|h) d[a-z] '
NMI watchdog Disabled (CPU0): Hardware events not enabled
SD 2:0:0:0: [SDA] 41943040 512-byte logical blocks: (21.4 gb/20.0 GiB)
SD 2:0:0:0: [SDA] Write Protect is off
SD 2:0:0:0: [SDA] Mode sense:61 00 00 00
SD 2:0:0:0: [SDA] Cache data unavailable
SD 2:0:0:0: [SDA] assuming drive cache:write through
SD 2:0:0:0: [SDA] Cache data unavailable
SD 2:0:0:0: [SDA] assuming drive cache:write through
SDA:SDA1 sda2
SD 2:0:0:0: [SDA] Cache data unavailable
SD 2:0:0:0: [SDA] assuming drive cache:write through
SD 2:0:0:0: [SDA] attached SCSI disk
Dracut:scanning devices sda2 for LVM logical volumes Vg_centos64min/lv_root Vg_centos64min/lv_swap
Ext4-fs (SDA1): mounted filesystem with ordered data mode. Opts:
selinux:initialized (Dev sda1, type Ext4), uses xattr
Displays the model number of the CPU:
[Email protected] ~]# Cat/proc/cpuinfo | Grep-i "Model"
Model:42
Model Name:intel (R) Core (TM) i5-2450m CPU @ 2.50GHz
[Email protected] ~]#
Of course, you can not apply pipes
[Email protected] ~]# grep-i ' Model '/proc/cpuinfo
Model:42
Model Name:intel (R) Core (TM) i5-2450m CPU @ 2.50GHz
[[email protected] ~]# grep ' Model '/proc/cpuinfo
[[email protected] ~]# grep ' model '/proc/cpuinfo
Model:42
Model Name:intel (R) Core (TM) i5-2450m CPU @ 2.50GHz
Just display the name of the file that matches the content
Grep-l ' main ' *.c
Or you can force grep to output in color:
grep--color vivek/etc/passwd
Linux command grep