三剑客之grep

三剑客之grep

1. grep [OPTIONS] PATTERN [FILE...]

2.相关参数

  • -n Prefix each line of output with the 1-based line number within its input file.

  • -v Invert the sense of matching, to select non-matching lines.

  • -E Interpret PATTERN as an extended regular expression (ERE, see below).

  • -o Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

  • -i Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)

  • -A Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

  • -B Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

  • -C Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

  • -w Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word- constituent characters are letters, digits, and the underscore

  • -R Read all files under each directory, recursively. Follow all symbolic links unlike -r.

  • -c Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)

  • -l Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)

3.示例

[root@gsh ~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@gsh ~]# 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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
[root@gsh ~]# grep -E "root|bash" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
gsh:x:1000:1000:gsh:/home/gsh:/bin/bash
gsh1:x:1002:1003::/home/gsh1:/bin/bash
TEST1:x:1099:1004::/home/TEST1:/bin/bash
test:x:1112:1112::/home/test:/bin/bash
[root@gsh ~]# grep -EO "root|bash" /etc/passwd
grep: invalid option -- 'O'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
[root@gsh ~]# grep -Eo "root|bash" /etc/passwd
root
root
root
bash
root
bash
bash
bash
bash
bash
[root@gsh ~]# grep -i "test" /etc/passwd
TEST1:x:1099:1004::/home/TEST1:/bin/bash
TEST2:x:1111:0::/home/TEST2:/bin/nologin
test:x:1112:1112::/home/test:/bin/bash
[root@gsh ~]# grep -A 1 "sshd" /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@gsh ~]# grep -B 1 "sshd" /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@gsh ~]# grep -C 1 "sshd" /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@gsh ~]# grep -w "gsh" /etc/passwd
gsh:x:1000:1000:gsh:/home/gsh:/bin/bash
[root@gsh ~]# grep "gsh" /etc/passwd
gsh:x:1000:1000:gsh:/home/gsh:/bin/bash
gsh1:x:1002:1003::/home/gsh1:/bin/bash
[root@gsh ~]# grep -R "echo" /etc/rc.local
#echo "大家好" >>/tmp/test.txt
[root@gsh ~]# grep -c "root" /etc/passwd
2
[root@gsh ~]# grep -l "sshd" `find /etc -type f`

 

posted @ 2020-09-30 21:46  Gsh-123  阅读(115)  评论(0)    收藏  举报