Use the Grep command

Source: Internet
Author: User

Use the Grep command
GuideAll linux systems provide a search tool named grep (global regular expression print, global regular expression output. The grep command is very useful when pattern-based searches are performed on the content of one or more files. The mode can be a single character, multiple characters, a single word, or a sentence. When the command matches the mode specified when the command is executed, grep outputs a line in the include mode, but does not modify the content of the original file.Example 1 search mode (Word) in the file)

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

root@Linux-world:~# grep linuxtechi /etc/passwdlinuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
Example 2 search for multiple files
root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow    /etc/passwd:linuxtechi:x:1000:1000: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 use the-L parameter to list the file names that contain the specified mode
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab    /etc/passwd    /etc/shadowroot@Linux-world:~#
Example 4 use the-N parameter to search for the specified mode in the file and display the row number that matches the row
root@Linux-world:~# grep -n linuxtechi /etc/passwd    39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
Example 5 use the-V parameter to output rows that do not contain the specified mode

Output all lines without the word "linuxtechi" in the/etc/passwd file

root@Linux-world:~# grep -v linuxtechi /etc/passwd
Example 6 use the ^ symbol to output all rows starting with a specified mode

The Bash script treats the ^ symbol as a special character to specify the beginning of a line or word. For example, output all rows starting with "root" in the/etc/passes File

root@Linux-world:~# grep ^root /etc/passwd    root:x:0:0:root:/root:/bin/bashroot@Linux-world:~#
Example 7 use the $ symbol to output all rows ending in the specified mode

Output all rows ending with bash in the/etc/passwd file

root@Linux-world:~# grepbash$ /etc/passwd    root:x:0:0:root:/root:/bin/bash    linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#

The Bash script treats the dollar ($) symbol as a special character to specify the end of a row or word.

Example 8 use the-R parameter to recursively find a specific mode
root@Linux-world:~# 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 recursively searches for the "linuxtechi" word in the/etc directory.

Example 9 use Grep to find all empty rows in the file
root@Linux-world:~# grep ^$ /etc/shadowroot@Linux-world:~#

No blank lines exist in the/etc/shadow file, so there is no output

Example 10 use the-I parameter Search Mode

The-I parameter of the grep command ignores the case sensitivity of characters during search. Let's look at an example to find the word "LinuxTechi" in the paswd file.

nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd    linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bashnextstep4it@localhost:~$
Example 11 use the-E parameter to find multiple modes

For example, if you want to find the 'linuxtechi' and 'root' words in a grep command and use the-e parameter, we can find multiple modes.

root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd    root:x:0:0:root:/root:/bin/bash    linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bashroot@Linux-world:~#
Example 12 use-F to specify the mode to be searched with a file

First, create a search mode file "grep_pattern" in the current directory.

root@Linux-world:~# cat grep_pattern    ^linuxtechi    root    false$root@Linux-world:~#

Now, try to search with the grep_pattern File

root@Linux-world:~# grep -f grep_pattern /etc/passwd
Example 13 use the-C parameter to calculate the number of matching Modes

In the above example, we use the-c command in the grep command to calculate the number of matched modes.

root@Linux-world:~# grep -c -f grep_pattern /etc/passwd    22root@Linux-world:~#
Example 14 output match the first or last N rows of the specified mode row

A) use the-B parameter to output the first four rows of matching rows.

root@Linux-world:~# grep -B 4"games" /etc/passwd

B) use the-A parameter to output the last four rows of matching rows.

root@Linux-world:~# grep -A 4"games" /etc/passwd

C) use the-C parameter to output four rows before and after matching rows

root@Linux-world:~# grep -C 4"games" /etc/passwd

From: http://www.tiejiang.org/14654.html

Address: http://www.linuxprobe.com/grep-command.html


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.