In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to achieve wildcards and regular expressions in linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Wildcard character
* any character, which can be repeated multiple times
? Any character, repeat it.
[] represents a character
For example: [a _ r _ b _ c] denotes any one of the abc
Wildcards are used to match file names
Regular expression
A regular expression matches a qualified string in a file.
Ls find cp does not support regular expressions
But grep awk sed supports regular expressions
[root@hadoop-bigdata01 test] # touch aa
[root@hadoop-bigdata01 test] # touch aab aabb
[root@hadoop-bigdata01 test] # ll
Total 0
-rw-r--r-- 1 root root 0 May 16 19:47 aa
-rw-r--r-- 1 root root 0 May 16 19:47 aab
-rw-r--r-- 1 root root 0 May 16 19:47 aabb
[root@hadoop-bigdata01 test] # ls aa
Aa
[root@hadoop-bigdata01 test] # ls aa?
Aab
[root@hadoop-bigdata01 test] # ls aa*
Aa aab aabb
Regular expression special character
Regular expression matching range
Regular expression standard character
Use regular expressions
Grep "1" / etc/passwd
For a line that contains the keyword 1, grep only needs to include it. You don't want wildcards, and you need to be exactly the same.
[root@hadoop-bigdata01 test] # grep "1" / etc/passwdbin:x:1:1:bin:/bin:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/ Gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinwang:x:501:501::/home/wang:/bin/bashgrep 'root '/ etc/passwdcat / etc/passwd | grep' root'
It's all the same thing, but pipe characters eat more resources.
So
1. Match rows that contain numbers
Grep'[0-9]'/ etc/passwd
two。 Match rows with three consecutive digits
Grep'[0-9] [0-9] [0-9]'/ etc/passwd or grep': [0-9] [0-9] [0-9]: / etc/passwd [root@hadoop-bigdata01 test] # grep'[0-9] [0-9] [0-9]'/ etc/passwdgames:x:12:100:games:/usr/games:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrtkit : x:499:497:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinsaslauth:x:498:76: "Saslauthd user": / var/empty/saslauth:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/ Pulse:/sbin/nologinliucheng:x:500:500::/home/liucheng:/bin/bashwang:x:501:501::/home/wang:/bin/bas
3. Match lines that begin with n and end with r
Grep'^ r.roomnails'/ etc/passwd.* stands for all [root@hadoop-bigdata01 test] # grep'^ r.roomnails'/ etc/passwd rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:497:RealtimeKit:/proc:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
4. Filter ifconfig and intercept ip
Grep-v stands for reverse interception, which means to remove the line with a keyword and sed means to replace.
[root@hadoop-bigdata01 test] # ifconfig | grep 'inet addr:' inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0 inet addr:127.0.0.1 Mask:255.0.0.0 [root@hadoop-bigdata01 test] # [root@hadoop-bigdata01 test] # ifconfig | grep' inet addr:' | grep-v '127.0.0.1' inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0 [root@hadoop-bigdata01 test] # ifconfig | grep 'inet addr:' | grep-v' 127.0.0.1'| sed 's/inet addr://g' 192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0 [root@hadoop-bigdata01 test] # ifconfig | grep' inet addr:' | grep-v '127.0.0.1' | sed' S/inet addr://g' | sed's Compact Bcast.Charpy Placement G' 192.168.126.191
Misunderstanding
There is a misunderstanding here. After thinking about it for a long time, it is the difference between regular expressions and wildcards.
We know that the * of a wildcard refers to any character, and the * of a repeatable regular expression refers to matching the previous character > = 0 times.
These two are completely different, so how do I know if the * I use is a wildcard or a regular expression?
At first I fell into a misunderstanding. Look at the following list of commands.
[root@hadoop-bigdata01 test] # touch ac aac abc abbc [root@hadoop-bigdata01 test] # lltotal 0 root root 0 May 16 19:55 aac-rw-r--r-- 1 root root 0 May 16 19:55 abbc-rw-r--r-- 1 root root 0 May 16 19:55 abc-rw-r--r-- 1 root root 0 May 16 19:55 ac [root@hadoop-bigdata01 test] # ls | grep 'a*c'aacabbcabcac [ Root@hadoop-bigdata01 test] # ls | grep 'a.*c'aacabbcabcac [root@hadoop-bigdata01 test] # ls | grep' ^ a.roomc'aacabbcabcac [root @ hadoop-bigdata01 test] # ls | grep'^ aroomc' aacac
Why are the results of grep'a wildcard 'and grep' ^ a wildcard different? I think one is a wildcard and the other is regular, because the four results shown by astatc are exactly the same.
Doesn't it just match any number of characters?
Actually this is not so
Wildcards are used to match file names
A regular expression matches a qualified string in a file.
After giving it to the pipe character, using grep does not match the file name, this is an operation on the file, so it is completely a regular expression.
Grep'astatc 'means to match a > = 0, so it's OK as long as it contains c.
And the grep'^ a'is also regular, indicating that it starts with a, and the second character matches a zero or more times, followed by the c letter.
So only aac and ac qualify.
So look at this example.
[root@hadoop-bigdata01 test] # lsa aac abbabbcabc ac bbbc cb [root@hadoop-bigdata01 test] # ls | grep 'a*b'abbabbcabcbbbcb
Here grep'aroomb 'does not mean that it contains an and b, but that a repeats 0 or more times and then contains b
Thank you for reading this article carefully. I hope the article "how to implement wildcards and regular expressions in linux" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.