Wildcards and regular expressions in linux, linux Regular Expressions
Wildcard
* Any character can be repeated multiple times
? Any character, repeat once
[] Represents a character
For example, [a, B, c] indicates any one of abc.
Wildcard is used to match the file name
Regular Expression
Regular expressions are used to match matching strings in the file.
Ls find cp does not support regular expressions
However, 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 -- 1 root 0 May 16 19:47 aa
-Rw-r -- 1 root 0 May 16 :47 aab
-Rw-r -- 1 root 0 May 16 :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
Special characters in Regular Expression
Regular Expression matching range
Regular Expression standard characters
Use Regular Expressions
grep "1" /etc/passwd
For a row that contains the keyword 1, grep only needs to contain the line. If you do not want a wildcard, it must be completely consistent.
[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'
The same is true, but pipeline operators are more resource-consuming.
So
1. Match rows containing numbers
grep '[0-9]' /etc/passwd
2. Match rows that contain three numbers consecutively
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 rows starting with "r" and ending with "n"
Grep '^ r. * n $ '/etc/passwd. * represents all [root @ hadoop-bigdata01 test] # grep '^ r. * n $ '/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 addresses
Grep-v indicates reverse truncation, which means to remove the line sed with a keyword.
[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/Bcast.*//g' 192.168.126.191
Misunderstanding
There is a misunderstanding here. For a long time, it is the difference between regular expressions and wildcards.
We know that wildcard * refers to any character, and repeated Regular Expressions * refer to matching the previous character> = 0
The two are completely different. How do I know whether * is a wildcard or a regular expression?
At first I got into a misunderstanding. Let's look at the following commands:
[root@hadoop-bigdata01 test]# touch ac aac abc abbc[root@hadoop-bigdata01 test]# lltotal 0-rw-r--r-- 1 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.*c'aacabbcabcac[root@hadoop-bigdata01 test]# ls | grep '^a*c' aacac
Why is the result of grep 'a * C' and grep '^ a * c $' different? I think one is a wildcard and the other is a regular expression, because a * c displays four results, exactly
Isn't it a match for any number of characters?
Otherwise
Wildcard is used to match the file name
Regular expressions are used to match matching strings in the file.
After being handed over to the pipeline operator, grep is no longer used to match the file name. This is an operation on the file, so it is completely a regular expression.
Grep 'a * C' indicates matching a> = 0, so it is acceptable if c is contained.
Grep '^ a * c $' is also a regular expression that begins with a, and the second character matches a zero or multiple times, followed by c letters
Therefore, only aac and ac meet the conditions.
So let's look at this example.
[root@hadoop-bigdata01 test]# lsa aac abb abbc abc ac b bb c cb[root@hadoop-bigdata01 test]# ls | grep 'a*b'abbabbcabcbbbcb
Here, grep 'a * B 'indicates that a does not contain a and B, but a repeats 0 times or multiple times and then contains B.
The above section describes the wildcards and regular expressions in linux. I hope this will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!