Common commands for text processing tool grep

Keywords: Linux git Unix

Grep is a powerful text search tool that searches for text using specific pattern matches, including regular expressions, and outputs matching lines by default.The grep family of Unix includes grep, egrep, and fgrep.

Format Usage: grep [Options]...Mode [File]...
Search mode in each file or standard input.
By default, a pattern is a basic regular expression (BRE).

Selection and interpretation of regular expressions:

-E pattern is an extended regular expression
-e pattern matches using pattern
-f Get mode from file
-i Ignore case differences
-w mandatory mode, matching only complete words

View the file as an example

root@zhaocheng ~]# cat filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
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
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
operator:x:11:0:operator:/root:/sbin/nologin

Grep-E is an extended regular expression that supports matching multiple elements and can be connected by a pipe character

[root@zhaocheng ~]# grep -E 'root|ROOT|sync' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

Grep-e is a standard match, one e can only match one element, pipeline is not supported, if you want to match more than one, you need to add-e to match later

[root@zhaocheng ~]# grep -e 'root' -e 'sync' filetest
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

There is also an e grep, which is an extended version of grep, that is, grep-e does not support multielement matching and pipelines, whereas e grep does support pipelines
It is understood that egrep (grep-E in linux) is an extended grep

[root@zhaocheng ~]# egrep 'root|sync' filetest
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
operator:x:11:0:operator:/root:/sbin/nologin

Grep-f This is generally used to sort out whether there are identical, duplicate rows in two files. If there are, duplicate rows are output.This / etc/passwd is also a file, and the previous filetest is also a file. Because I deleted part of the filetest to make a comparison, the effect will be obvious. In general, if two files are checked for identical, the same lines are filtered out, you can use grep -f

Format grep-f file 1 file 2

[root@zhaocheng ~]# grep -f filetest /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
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
operator:x:11:0:operator:/root:/sbin/nologin

Grep-i ignores case for matching, this''can also be omitted

[root@zhaocheng ~]# grep -i 'root' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

If you ignore case and want to do more matching, you can use egrep, which is equivalent to grep-E, to support piping and multiline matching

[root@zhaocheng ~]# egrep -i 'root|halt' filetest
ROOT:x:98:0:ROOT:/ROOT:/usr/local
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin

The matches used earlier are all matching rows, and you only want to match one word, you can use -w,

[root@zhaocheng ~]# egrep -w 'halt|sync' filetest
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
[root@zhaocheng ~]# egrep -w 'hat|syn' filetest

** Some other parameters

-s No message suppress error message
-v Anti-matching selects rows that do not match
-V Displays version information and exits
--help Display this help text and exit**

-s does not display error information, for example, without this file, -s does not output error content

[root@zhaocheng ~]# grep kkkk /etc/shadows
grep: /etc/shadows: No such file or directory
[root@zhaocheng ~]# grep -s kkkk /etc/shadows

-v Output all data except lp

[root@zhaocheng ~]# grep -v lp filetest
linuxaweqeeqw
ROOT:x:98:0:ROOT:/ROOT:/usr/local
halt$: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
#sync:x:5:0:sync:/sbin:/bin/sync
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
linuxoooghhrhg

-V Outputs grep version information and exits

[root@zhaocheng ~]# grep -V
grep (GNU grep) 2.20
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

Posted by Jeremias on Tue, 18 Feb 2020 08:38:17 -0800