In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "how to use grep in Linux". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to use grep in Linux" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.
Grep full name Global Regular Expression Print, means that global regular expressions is a powerful text search tool, using regular matching, and sed, awk called Linux Three Musketeers.
1. Command format grep [options] files
2. Main parameter-c: only output the number of matching lines-i: case-insensitive-n: display matching lines and line numbers-l: only output file names containing matching characters when querying multiple files-v: reverse matching, i.e. display unmatched lines-h: not applicable when querying file names-s: no error message is displayed
3. Partial regular expressions\antisense characters: such as """means match" ^$start and end [] single character,[A] [-] matches a range,[0-9a-zA-Z] matches all numbers and letters
0 or more occurrences of the preceding character
The preceding character appears one or more times. any character
4. Classic Scenes
Unless you want to be case sensitive exactly, add-i to ignore case
(1) Combine the find command with the pipeline You have a music folder with files in multiple formats, and you only want to find the mp3 files of artist jay, and do not contain any mixed tracks
[root@localhost ~]#find . -name ".mp3" | grep -i jay |grep -vi "remix" analysis: 1) use find -name to list all mp3 files, redirect to grep
Use grep -i to find rows that contain jay 3) Use grep -vi to find rows that do not contain remix
(2)-A -B -C Many times we are not concerned with matching rows but with the context of matching rows. A -B -C is useful in this case-A n after n lines, A memory is (After) -B n before n lines, B memory is (Before) -C n before n lines, after n lines, C memory is (Center) Example
[root@localhost ~]# ifconfig | grep -A 2 "Link encap" eth0 Link encap:Ethernet HWaddr 00:0C:29:F3:38:15 inet addr:192.168.91.129 Bcast:192.168.91.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link -- lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host [root@localhost ~]# ifconfig | grep -C 2 "lo" Interrupt:67 Base address:0x2024 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr::1/128 Scope:Host(3) Count with-c You have a large file with a URL, such as www.baidu.com tieba.baidu.com, etc. Do you want to know how many websites belong to Baidu?
[root@localhost ~]# grep -c "*baidu.com*" filename example [root@localhost ~]# cat file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 162.12.0.123 "123" 123"" 123 www.baidu.com/search/index [root@localhost ~]# grep -cn ".* www.google.com tieba.baidu.com www.baidu.com njuhwc@gmil.com njuhwc@163.com baidu.com.* " file.txt 3(4) -r Recursive search subdirectories Find the current directory and the files containing matching characters under the subdirectories Find subdirectories, output the line number after matching, where the dot indicates the current directory
[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* . Examples:
[root@localhost ~]# grep -nr baidu . ./ file.txt:8:www.baidu.com ./ file.txt:9:tieba.baidu.com ./ file.txt:11:www.baidu.com/search/index ./ test/test.txt:1:http://www.baidu.com Find the subdirectory, and only output the file name after matching
[root@localhost ~]# grep -lr HELLO_HWC_CSND_BLOG* . Examples:
[root@localhost ~]# grep -lr baidu . ./ file.txt ./ test/test.txt (5)-line-buffered Turn on buffering mode You have a file that is dynamic, it constantly adds information to the end of the file, and you want to output lines that contain some information. a continuous grep, a dynamic stream.
[root@localhost ~]#tail -f file |grep --line-buffered your_pattern(6) combined with ps lookup process
[root@localhost ~]# ps aux | grep init root 1 0.0 0.1 2072 632 ? Ss 22:52 0:01 init [5] root 4210 0.0 0.1 6508 620 ? Ss 23:01 0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients" root 4233 0.0 0.0 2780 504 ? S 23:01 0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients root 4956 0.0 0.1 3920 680 pts/1 R+ 23:27 0:00 grep init Here we see grep init and the commands we execute are listed. If we don't want this line, we can change the command like this.
[root@localhost ~]# ps aux | grep [i]nit root 1 0.0 0.1 2072 632 ? Ss 22:52 0:01 init [5] root 4210 0.0 0.1 6508 620 ? Ss 23:01 0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients" root 4233 0.0 0.0 2780 504 ? S 23:01 0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients(7) Find a directory that does not contain a directory
[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search example
[root@localhost ~]# ls anaconda-ks.cfg Desktop file.txt find.result install.log install.log.syslog test [root@localhost ~]# grep -r baidu . ./ file.txt:www.baidu.com ./ file.txt:tieba.baidu.com ./ file.txt:www.baidu.com/search/index ./ test/test.txt:http://www.baidu.com At this time if we do not want to include the test directory
[root@localhost ~]# grep -R --exclude-dir=text "baidu" . ./ file.txt:www.baidu.com ./ file.txt:tieba.baidu.com ./ File.txt: www.baidu.com/search/index.html
grep: unrecognized option `--exclude-dir=test'indicates that the version is too old, ok after updating
(8)-o, -only-matching: Show only the part of a matching line that matches PATTERN. -P, –perl-regexp: Interpret PATTERN as a Perl regular expression. That is, -o, displays only the part of the matching row that matches the regular expression-P, as a Perl regular match
[root@localhost ~]# cat file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 162.12.0.123 "123" 123""123 njuhwc@163.com njuhwc@gmil.com 123 www.baidu.com tieba.baidu.com www.google.com www.baidu.com/search/index [root@localhost ~]# grep -oP "([0-9]{1,3}\.) {3}[0-9]{1,3}" file.txt 192.168.0.1 162.12.0.123 (9) Find Email
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\. [a-zA-Z0-9_-]+)+"file.txt Example
[root@localhost ~]# cat file.txt wtmp begins Mon Feb 24 14:26:08 2014 192.168.0.1 162.12.0.123 "123" 123""123 njuhwc@163.com njuhwc@gmil.com 123 www.baidu.com tieba.baidu.com www.google.com www.baidu.com/search/index [root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\. [a-zA-Z0-9_-]+)+" file.txt njuhwc@163.com njuhwc@gmil.com Read here, this article" How to use grep in Linux "article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.