Instance of grep command under Linux

Time:2021-12-30

Grep command parameters

Grep ‘word’ file name
Grep ‘word’ file 1 file 2 file 3… File n
Grep ‘string 1 string 2’ file name
Cat file name “grep” string“
Command | grep “string”
Grep – color “string” file name

How to use the grep command to search the contents of a file?

To find out whether the user user1 exists in the / etc / passwd file, you can use the following command:

Copy code

The code is as follows:

[[email protected] ~]# grep user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
[[email protected] ~]#

If you want to ignore case to find, such as user1, user1 or other combinations, you can use the – I option

Copy code

The code is as follows:

[[email protected] ~]# grep -i user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[[email protected] ~]#

Grep command recursive lookup

You can recursively find the rows matching the search pattern in all levels of subdirectories under a directory, and use the – R option

Copy code

The code is as follows:

[[email protected] ~]# grep -r user1 /etc | more
/etc/group-:user1:x:501:
/etc/shadow:user1:!!:16176:0:99999:7:::
/etc/group:user1:x:501:
/etc/passwd-:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow-:user1:!::
/etc/shadow-:user1:!!:16176:0:99999:7:::
/etc/passwd:user1:x:501:501::/home/user1:/bin/bash
/etc/gshadow:user1:!::
/etc/postfix/virtual:# [email protected] address1
[[email protected] ~]#

Use grep to match a word

Option – W causes the grep command to find matches only as a word, not as part of a word

For example:

Copy code

The code is as follows:

[[email protected] ~]# grep user1 /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
user1add:x:503:503::/home/user1add:/bin/bash
[[email protected] ~]# grep -w ‘user1′ /etc/passwd
user1:x:501:501::/home/user1:/bin/bash

In this example, there are two lines of patterns that can match “user1”, so when you only want to match a complete word of user1, you can use the – W option.

Use the grep command to find 2 different words

Copy code

The code is as follows:

[[email protected] ~]# egrep -w “user1|USER1″ /etc/passwd
user1:x:501:501::/home/user1:/bin/bash
USER1:x:502:502::/home/USER1:/bin/bash
[[email protected] ~]#

Count the number of rows found by grep

The grep command can use the – C option to display the number of rows matched

Copy code

The code is as follows:

[[email protected] ~]# grep -c ‘user1′ /etc/passwd
2

Use the – N option to display the line number of the line found in the file

Copy code

The code is as follows:

[[email protected] ~]# grep -n ‘user1′ /etc/passwd
23:user1:x:501:501::/home/user1:/bin/bash
25:user1add:x:503:503::/home/user1add:/bin/bash

Grep reverse lookup shows only mismatched rows

You can use the – V option to display unmatched rows, for example:

Copy code

The code is as follows:

[[email protected] ~]# grep -v user1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
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

Combination of Linux pipeline character and grep command

Grep command is often used with Linux or UNIX pipeline characters, such as displaying the name of hard disk device:

Copy code

The code is as follows:

[[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_devops/lv_root vg_devops/lv_swap
EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts:
SELinux: initialized (dev sda1, type ext4), uses xattr

Display CPU module name:

Copy code

The code is as follows:

[[email protected] ~]# cat /proc/cpuinfo | grep -i ‘Model’
model : 42
model name : Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
[[email protected] ~]#

How to output only the file name containing the schema instead of the text line

Copy code

The code is as follows:

[[email protected] ~]# grep -l ‘user1′ /etc/*
/etc/group
/etc/group-
/etc/gshadow
/etc/gshadow-
/etc/passwd
/etc/passwd-
/etc/shadow
/etc/shadow-