Linux three swordsmen grep
describe
grep – global search regular expression(RE) and print out the line;
grep is a text search tool, which can use regular expressions to filter or search text and print matching lines;
Mode of use
-
Common options
option describe -A <n> Display the filtered text line and the n lines after the line -B <n> Display the filtered text line and the n lines before the line -C <n> The filtered text line is displayed, and each n line before and after the line is displayed -c Calculate the number of lines that match the text -e Used to specify multiple strings for filtering -E Filtering with extended regular expressions -f Get the string from the specified file for filtering, one by one -i Ignore case of filtered characters -n Indicates the line number that matches the filtered text -v Flip the search to display rows that do not match the filtered text -o Output only matching strings -m <num> Stop searching after matching num row results -q In silent mode, no information is output for script judgment -
Common examples
- Filter the text line of the matching rule and display its upper and lower lines at the same time
[root@centos-36_2 tmp]# cat test_grep The Redis server has been running, ignore this step. 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ 2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: Index: 3 CoreId: Broad0 CoreIpAddress: 172.16.36.218 IsPrincipal: false CoreName: broad_Aux 2021-07-29 15:18:15-orr.py-553-ERR: Not Get RPD IRA-RSP [root@centos-36_2 tmp]# grep -C 2 'abc' test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ [root@centos-36_2 tmp]#
- Filter the text line of the matching rule and display the line number at the same time
[root@centos-36_2 tmp]# grep -n 'abc' test_grep 4:2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 [root@centos-36_2 tmp]#
- Displays the number of lines of text for the filter matching rule
[root@centos-36_2 tmp]# grep -n 'NOTIFY' test_grep 3:2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 7:2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: [root@centos-36_2 tmp]# grep -c 'NOTIFY' test_grep 2 [root@centos-36_2 tmp]#
- Set multiple matching rules for filtering
[root@centos-36_2 tmp]# grep -e "INFO" -e "NOTIFY" test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ 2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: [root@centos-36_2 tmp]#
- Filtering using regular expression matching rules
[root@centos-36_2 tmp]# grep -E "^Core.*[0-9]{1,3}$" test_grep CoreId: Broad0 CoreIpAddress: 172.16.36.218 [root@centos-36_2 tmp]# grep -E "^Core.*[0-9]{2,3}$" test_grep CoreIpAddress: 172.16.36.218 [root@centos-36_2 tmp]#
- Ignore case when filtering matching rules
[root@centos-36_2 tmp]# grep 'info' test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# grep -i 'info' test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ [root@centos-36_2 tmp]#
- Flip lookup to filter rows that do not conform to the rules
[root@centos-36_2 tmp]# grep -v "[0-9]" test_grep The Redis server has been running, ignore this step. IsPrincipal: false CoreName: broad_Aux [root@centos-36_2 tmp]#
- Get matching rules from files for filtering
[root@centos-36_2 tmp]# cat grep_txt INFO NOTIFY [root@centos-36_2 tmp]# grep -f grep_txt test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ 2021-07-29 13:15:55-tor.py-550-NOTIFY: RPD IRA-RSP CORE-ID TLVs: [root@centos-36_2 tmp]#
- Output matching string
[root@centos-36_2 tmp]# grep -E "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep CoreIpAddress: 172.16.36.218 [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep 172.16.36.218
- Filter multiple files according to matching rules
[root@centos-36_2 tmp]# cp test_grep test_grep_bak [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# grep -oE "([0-9]{1,3}\\.){3}[0-9]{1,3}$" test_grep test_grep_bak test_grep:172.16.36.218 test_grep_bak:172.16.36.218 [root@centos-36_2 tmp]#
- Filter according to the matching rules and stop the display after m are displayed
[root@centos-36_2 tmp]# grep -i "info" test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info 2021-07-29 12:33:44-abc.py-542-INFO: ===> rcp_msg_id: 2 2021-07-29 13:15:15-kln.py-478-INFO: Received NotifyREQ [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# grep -i -m1 "info" test_grep 2021-07-29 11:31:34-tor.py-478-NOTIFY: Received info [root@centos-36_2 tmp]#
- Use of silent mode in scripts
[root@centos-36_2 tmp]# cat grep_q.sh #!/bin/bash grep -q ERR ./test_grep exist_err=$? echo ${exist_err} if [[ ${exist_err} -eq 0 ]]; then echo "has error log" else echo "no error log" fi [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# grep ERR ./test_grep 2021-07-29 15:18:15-orr.py-553-ERR: Not Get RPD IRA-RSP [root@centos-36_2 tmp]# [root@centos-36_2 tmp]# ./grep_q.sh 0 has error log [root@centos-36_2 tmp]#