Example of a 14 grep command

Source: Internet
Author: User
Tags syslog

Overview:

All Linux-class systems provide a search tool called grep (Global Regular expression print, which is the output of globally regular expressions). The grep command is useful in situations where a pattern-based search is performed on the contents of one or more files. A pattern can be a single character, multiple characters, a single word, or a sentence.

When the command matches the pattern specified when the command is executed, grep outputs a line containing the pattern, but does not modify the contents of the original file.

In this article, we will discuss 14 examples of GREP commands.

Example 1 finding a pattern in a file (word)

Find the word "Linuxtechi" in the/etc/passwd file

Root@Linux-World:~/etc/passwdlinuxtechi:x:+: : Linuxtechi,,,:/Home/Linuxtechi:/bin/bashroot @Linux -World:~#
Example 2 finds a pattern in multiple files.
Root@Linux-World~# grep Linuxtechi/etc/passwd/etc/Shadow/etc/Gshadow/etc/PASSWD:LINUXTECHI:X: +: +: Linuxtechi,,,:/Home/Linuxtechi:/Bin/Bash/etc/shadow:linuxtechi:$6$DdgXjxlM $4flz4jrvefvkp0dg6re:16550:0:99999:7:::/etc/Gshadow:adm:*:: Syslog,linuxtechi/etc/Gshadow:cdrom:*:: Linuxtechi/etc/Gshadow:sudo:*:: Linuxtechi/etc/Gshadow:dip:*:: Linuxtechi/etc/Gshadow:plugdev:*:: Linuxtechi/etc/Gshadow:lpadmin:!::linuxtechi/etc/gshadow:linuxtechi:!::/etc/Gshadow:sambashare:!::linuxtechiroot@Linux-World~#
Example 3 uses the-l parameter to list the file name of the file that contains the specified pattern.
[Email protected]:~# grep-l Linuxtechi/etc/passwd/etc/shadow/etc/fstab/etc/mtab/etc/passwd/etc/shadow[email protected]:~#
Example 4 uses the-n parameter to find the specified pattern in the file and displays the line number of the matching row
[Email protected]:~# grep-n Linuxtechi/etc/passwd39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/ Bash[email protected]:~#
[Email protected]:~# grep-n Root/etc/passwd/etc/shadow

Example 5 using the-v parameter to output rows that do not contain a specified pattern

Output all lines in the/etc/passwd file that do not contain the word "Linuxtechi"

R[email protected]:~# grep-v linuxtechi/etc/passwd

Example 6 using the ^ symbol to output all lines beginning with a specified pattern

The bash script sees the ^ symbol as a special character used to specify the beginning of a line or a word. For example, all lines that start with "root" in the output/etc/passes file

[Email protected]:~# grep ^root/etc/passwdroot:x:0:0:root:/root:/bin/bash[email protected]:~#
Example 7 uses the $ symbol to output all lines ending in the specified pattern.

Output all lines in the/etc/passwd file that end with "bash".

[Email protected]:~# grep bash$/etc/passwdroot:x:0:0:root:/root:/bin/bashlinuxtechi:x:1000:1000:linuxtechi,,,:/ Home/linuxtechi:/bin/bash[email protected]:~#

The bash script sees the dollar ($) symbol as a special character used to specify the end of a line or a word.

Example 8 using the-R parameter to recursively find a specific pattern
[Email protected]:~# grep-r Linuxtechi/etc//etc/subuid:linuxtechi:100000:65536/etc/group:adm:x:4:syslog, linuxtechi/etc/group:cdrom:x:24:linuxtechi/etc/group:sudo:x:27:linuxtechi/etc/group:dip:x:30:linuxtechi/etc/ Group:plugdev:x:46:linuxtechi/etc/group:lpadmin:x:115:linuxtechi/etc/group:linuxtechi:x:1000:/etc/group: Sambashare:x:131:linuxtechi/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash/etc/ Passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash ............................................................................

The above command will recursively look for "linuxtechi" words in the/etc directory

Example 9 using grep to find all empty lines in a file
[Email protected]:~# grep ^$/etc/shadow[email protected]:~#

Because there are no blank lines in the/etc/shadow file, there is no output

Example 10 using the-I parameter lookup mode

The-i parameter of the grep command ignores the case of characters when looking.

Let's look at an example to find the word "Linuxtechi" in the paswd file.

[Email protected]:~$ grep-i Linuxtechi/etc/passwdlinuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash[email protected]:~$
Example 11 finding multiple modes using the-e parameter

For example, I want to find the ' Linuxtechi ' and ' root ' words in a grep command, and with the-e parameter, we can find multiple patterns.

[Email protected]:~# grep-e "Linuxtechi"-E "root"/etc/passwdroot:x:0:0:root:/root:/bin/bashlinuxtechi:x:1000:1000: Linuxtechi,,,:/home/linuxtechi:/bin/bash[email protected]:~#
Example 12 using the-F file to specify the mode to find

First, create a search mode file "Grep_pattern" in the current directory, and I want to enter the following content in the file.

[Email protected]:~# cat Grep_pattern^linuxtechirootfalse$[email protected]:~#

Now, try searching using the Grep_pattern file

[Email protected]:~# grep-f grep_pattern/etc/passwd


Example of a 14 grep command

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.