Linux text processing three swordsmen's grep detailed explanation

One, grep tool introduction

    As long as the Linux system grep command is used to find qualified strings in a file, through the cooperation of various options, it can search and match patterns to meet our needs.
    The grep family of Unix includes grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from grep. egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They treat all letters as words, that is, metacharacters in regular expressions represent their own literal meaning , No longer special. Linux uses the GNU version of grep. It is more powerful, you can use the functions of egrep and fgrep through the -G, -E, and -F command line options.
    Enter grep --help on the command line of the Linux system to view the help documentation of grep. The grep command has many options. You don’t need to remember all of them in normal use. Mastering the common options is enough.

Two, grep command commonly used options

Options usage
-a Search for data in binary file as text file
-c Count the number of times the'search string' is found
-d When you specify that you want to find a directory instead of a file, use the -d parameter. There are three valid parameters after the -d parameter: read (read), recurse (recursive), skip (skip), and use the -d parameter When you need to add one of the first three parameters according to the actual situation
-f Specify the sample file. If you want to search for "root" and "ops" in a file, you can add "root" and "ops" to a file, one sample per line, this file is the sample file, the command format: grep -f sample file target file
-i Ignore the difference in case, all cases are treated as the same letter
-n Output the line number of the line where the matched string is located
-O Only the part that is matched by the matching pattern is output. If the matching pattern does not match a whole line, it will not output a whole line
-q Silent output, no information is displayed
-r The effect of this parameter is the same as specifying the "-d recurse" parameter
-v Inverted (reverse search), that is, the line without the content of the'search string' is displayed
-w Exact matching, the matched text can only be a word, not a certain part of the word. For example, in the search text, interester and interest are on different lines, and I only want to find the line that contains interest. This is fine Use the -w option to specify that the search line is the line containing interest, so that you can avoid matching the interester line
\b Boundary character, word lock character, such as: \bxxxx\b only matches xxxx, similar to -w option
-A Output the line where the search string is located and the next n lines (grep -A n "string" filename), after
-B Output the line where the search string is located and the previous N lines (grep -B n "string" filename), before
-C Output the line where the search string is located and the n lines before and after it ((grep -C n "string" filename)), context
-E PATTERN (pattern) is an extended regular expression (ERE), open support for extended regular expression, grep -E is equivalent to egrep command
-F PATTERN (pattern) is a set of fixed strings separated by line breaks. Use this parameter to treat the contents of a sample file as a list of fixed strings
-G PATTERN (pattern) is a basic regular expression (BRE)
--color=auto You can add color to the key words found

Three, examples

1. Take out the line containing root in the /etc/passwd file

#grep "root" /etc/passwd
或
#cat /etc/passwd | grep "root" 

2. Use the -c option to count the number of lines containing root in the /etc/passwd file

#grep "root" /etc/passwd | wc -l
或
#cat /etc/passwd | grep "root" wc -l

3. Use the -d and -r options to find out the file content of the file
tmp.txt containing ops in the /tmp directory :
cat tmp.txt
ops
ops_11111

#查找
#grep -d recurse "ops" /tmp 或 grep -r "ops" /tmp
#输出内容如下:
/tmp/tmp.txt:ops
/tmp/tmp.txt:ops_11111
#提取文件名
#grep -d recurse "ops" /tmp | awk -F: '{print $1}' 或 grep -r "ops" /tmp | awk -F: '{print $1}'
#输出内容如下:
/tmp/1.txt
/tmp/1.txt

4. Using the -f option, the output /etc/passwd file always contains the content of ops and the line
username.txt containing root :
cat username.txt ---->user.txt as the sample file
ops
root

#查询
#grep -f username.txt /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ops:x:1002:1002::/home/ops:/bin/bash

5. The use of -i option is case-insensitive, find out
the file content of test.txt containing A or a in the test.txt file:
cat test.txt
A111
a111
B111

①不加-i选项
#grep 'a' test.txt
a111
②加-i选项
#grep -i 'a' test.txt
A111
a111

6. The use of -n option, output the line containing ops in the /etc/passwd file, and output the line number of the line

#grep -n "ops" /etc/passwd
27:ops:x:1002:1002::/home/ops:/bin/bash

7. Use the -o option to output
the file content of the word hello in the test1.txt file:
cat test1.txt
hello world
my name is beijing

#grep -o "hello" test1.txt
hello ----->只输出模式匹配到的单词,而不是含有该单词的一整行

8. The use of -v option, output lines that do not contain /root/ in the /etc/passwd file

#grep -v "root" /etc/passwd

9. Using the -w option, output the content of the test2.txt file containing interest in the
test2.txt file:
cat test2.txt
interest ghaha
interester ghaha

①不加-w选项
#grep "interest" test2.txt
interest ghaha
interester ghaha
②加上-w选项
#grep -w "interest" test2.txt 
interest ghaha

10. The use of \b boundary character, output the line containing interest in test2.txt file

#grep "\binterest\b" test2.txt
interest ghaha

11. The use of -A option, output the line containing ops and the following line in the /etc/passwd file

#grep -A 1 "ops"  /etc/passwd
ops:x:1002:1002::/home/ops:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

12. The use of -B option, output the line containing ops and the line before it in the /etc/passwd file

#grep -B 1 "ops"  /etc/passwd
hahaha:x:1001:1001::/home/hahaha:/bin/bash
ops:x:1002:1002::/home/ops:/bin/bash

13. The use of -C option, output the line containing ops in the /etc/passwd file and one line before and after it

#grep -C 1 "ops" /etc/passwd
hahaha:x:1001:1001::/home/hahaha:/bin/bash
ops:x:1002:1002::/home/ops:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

Use the grep command in combination with
regulars
. If you don’t understand regulars, you can take a look at this: https://blog.csdn.net/weixin_44901564/article/details/99075443 1. Extract the files that start and end with the same letter in the /etc/passwd file Row

#grep `"^\([[:alpha:]]\).*\1$"` /etc/passwd 
#使用了正则分组及引用的知识,没有使用扩展正则,所以可以加不加-E选项都可以

2. Extract all ips in the network configuration file /etc/sysconfig/network-scripts/ifcfg-ens33

#grep -Eo "([[:digit:]]+\.){3}[[:digit:]]+" /etc/sysconfig/network-scripts/ifcfg-ens33 
#使用了基本正则,扩展正则,分组与添加grep适当的选项

3. Extract the parent directory name of the /etc/sysconfig/network-scripts/ directory

#echo "/etc/sysconfig/network-scripts/" |egrep -o "/.*[^/]" |egrep -o "/.*/"
/etc/sysconfig/
分为两步提取,第一步是先删除最后一个"/",目的是方便第二次的匹配模式的确定,第二步直接提取

Guess you like

Origin blog.csdn.net/weixin_44901564/article/details/104556127