Linux wildcard characters and regular expressions

Source: Internet
Author: User
Tags gopher

Wildcard characters* Any character, can be repeated multiple times? Any character, repeat once
[] represents a characterexample: [a,b,c] means any one of the ABCwildcard characters are used to match the file name of theRegular ExpressionsA regular expression is a string that matches a condition in a file .LS find CP is not supported for regular expressionsbut grep awk sed supports regular expressions
[[email protected] test]#Touch AA[[email protected] test]#Touch AaB Aabb[[email protected] test]#llTotal 0-rw-r--r--1 root root 0 May 16 19:47AA-rw-r--r--1 root root 0 May 16 19:47AaB-rw-r--r--1 root root 0 May 16 19:47Aabb[[email protected]-BIGDATA01 Test]#ls AAAa[[email protected]-BIGDATA01 Test]#ls AA?Aab[[email protected]-BIGDATA01 Test]#ls aa*AA AaB Aabb
Regular Expression Special charactersRegular Expression Match range

Regular expression Standard characters

using regular Expressionsgrep "1"/etc/passwdthe line containing the keyword 1, grep as long as it contains the line, do not want wildcards, to be exactly the same
[[email protected] 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/bash
grep ' root '/etc/passwd
cat/etc/passwd | grep ' root '
That's the same thing, but pipe breaks eat more resources.so1. Match rows with numbers grep ' [0-9] '/etc/passwd 2. Match rows with three digits in a row grep ' [0-9][0-9][0-9] '/etc/passwd or grep ': [0-9][0-9][0-9]: '/etc/ passwd
[[email protected] 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 ending with R beginning with n grep ' ^r.*n$ '/etc/passwd
. * On behalf of all
[[email protected] 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, intercept IPGrep-v represents a reverse intercept, meaning to remove a line with a certain key word sed has the meaning of substitution
[[email protected] test]#ifconfig | grep ' inet addr: 'inet addr:192.168.126.191 bcast:192.168.126.255 mask:255.255.255.0inet Addr:127.0.0.1 mask:255.0.0.0[[Email protected]-BIGDATA01 Test]#[[email protected] 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[[Email protected]-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[[Email protected]-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
Misunderstandinghere is a misunderstanding, think for a long time, is the difference between regular expression and wildcard characterswe know that the wildcard * refers to any character that can be repeated multiple times the regular expression of * refers to the match of the previous character >=0 times these two are completely different, how do you know if I'm using a wildcard or regular expressionat first I fell into a misunderstanding, see the following sequence of commands
[[email protected] test]#Touch AC AAC ABC ABBC[[email protected] test]#llTotal 0-rw-r--r--1 root root 0 May 16 19:55AAC-rw-r--r--1 root root 0 May 16 19:55ABBC-rw-r--r--1 root root 0 May 16 19:55ABC-rw-r--r--1 root root 0 May 16 19:55Ac[[email protected]-BIGDATA01 Test]#ls | grep ' a*c 'Aacabbcabcac[[email protected]-BIGDATA01 Test]#ls | grep ' a.*c 'Aacabbcabcac[[email protected]-BIGDATA01 Test]#ls | grep ' ^a.*c 'Aacabbcabcac[[email protected]-BIGDATA01 Test]#ls | grep ' ^a*c 'AACAC
why grep ' a*c ' and grep ' ^a*c$ ' results will be different, I think one is wildcard, one is regular, because a*c shows four results, justdoes it match any number of characters ? not actuallywildcard characters are used to match the file name of theA regular expression is a string that matches a condition in a file .The use of grep after the pipe break is not a matching file name, this is the operation of the files, so that he is exactly the regular expressiongrep ' A*c ' represents a match a>=0 so as long as C is availablegrep ' ^a*c$ ' is also regular, which means that the second character matches a 0 or more times, followed by the C-letterso only AAC and AC meet the criteriaso look at this example
[[email protected] test] # ls a  AAC  ABB  ABBC  ABC  ac  b  bb  c  cb[[email protected]-BIGDATA01 Test]#  ls | grep ' a*b 'ABBABBCABCBBBCB

Here grep ' a*b ' refers not to A and B but a repeats 0 or more times and then contains B

Linux wildcard characters and regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.