What is grep?
grep (Global search Regular expression (RE) and print out of the line, whose full meaning is global searching for regular expressions, is printed out. is a powerful, easy-to-use text Search tool. It can match the text in the file according to the matching method, and print the whole line that matches.
in Linux, the grep family has three members, respectively, Grep,egrep and Fgrep, which are used slightly differently, where egrep is the extension of grep, while fgrep is processing all the letters as words when making text-to-pass. Let the meta-characters in the regular expression lose meaning and treat them as ordinary words. The Egrep and FGREP functions can be implemented with grep followed by the-E or-f option when used.
Common options and general usage for grep.
The frequently used options for grep are the following
-V: Reverse Displays the lookup text, that is, matches to the instead of display, displays the characters that have not been matched.
Example: "Hello" string in normal match file test
[[email protected] tmp]# grep "Hello" test
Hello
Plus-v option
[[email protected] tmp]# grep-v "Hello" test
This is a test txt
Welcome to use grep
-I: Ignore case in text match
[email protected] tmp]# grep-i " How is You" test
How is You
How is You
-N: Displays the line number when the text is output
[email protected] tmp]# grep-n " How is You" test
5:how is You
-C: Shows the number of times the match was matched
[email protected] tmp]# Grep-ic " How is You" test
2
--color=auto: Use this option to display the string that is matched to
[email protected] tmp]# grep--color=auto "how" Test
how is you
Other options are no longer mentioned, and can be viewed by viewing the grep help document.
grep combines regular expressions for fast and accurate matching of text.
Combined with grep and regular expressions, you can quickly and accurately find the strings and rows you want to match and speed up your work.
Example 1: Find the row in the/etc/passwd file where root is located
[[email protected] tmp]# grep--color=auto "^root"/etc/passwd
Root : X:0:0:root:/root:/bin/bash
Example 2: Find the SYSTEM account in the/TEC/PASSWD file, display its user name and UID
[Email protected]p tmp]# cut-d:-f1,3/etc/passwd|grep--color=auto "\<[1-4[0-9][0-9]\>"
UUCP:ten
operator: One
Games :
Gopher:
ftp:
Nobody:
Dbus:bayi
VCSA:
Haldaemon:
GDM:
NTP:
Apache:
postfix:
sshd:
tcpdump:
named:
Example 3: Taking the base name of a string/etc/sysconfig/
[[ Email protected] tmp]# echo/etc/sysconfig |grep-e-o "/[[:alpha:]]*$" | grep-o-E "[[: alnum:]]*]
sysconfig
Example 4: Match a reasonable IP address in the Testip file
[email protected] tmp]# cat Testip
192168.0.1
192.155.234.2
256.234.245.654
123.234.212.12
1.0.0.1
[email protected] tmp]# cat Testip |grep--colorauto-e "(^\b[1-9]\b|^\b[1-9][0-9]\b|^\b1[0-9][0-9]\b|^\b2[1-3[ 0-9]) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) \. (\b[0-9]\b|\b[1-9][0-9]\b|\b1[0-9][0-9]\b|\b2[0-4][0-9]\b|\b25[0-5]\b) $ "
192.155.234.2
123.234.212.12
1.0.0.1
This article is from the "linux-Bubble" blog, make sure to keep this source http://7703592.blog.51cto.com/7693592/1627444
Using grep and regular expression to realize text-to-match quickly and precisely