Linux通配符与正则表达式

[root@hadoop-bigdata01 test]# touch ac aac abc abbc [root@hadoop-bigdata01 test]# ll total 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' aac abbc abc ac [root@hadoop-bigdata01 test]# ls | grep 'a.*c' aac abbc abc ac [root@hadoop-bigdata01 test]# ls | grep '^a.*c' aac abbc abc ac [root@hadoop-bigdata01 test]# ls | grep '^a*c' aac ac

为什么grep 'a*c'  和 grep '^a*c$' 的结果会不一样,我以为一个是通配符,一个是正则,因为a*c显示的四个结果,正好

不就是匹配任意多个字符吗?

其实不然

通配符的作用是用来匹配文件名的

正则表达式是在文件中匹配符合条件的字符串的

交给管道符之后使用grep已经不是匹配文件名了,这是对文件的操作,所以说,他完全就是正则表达式

grep 'a*c' 表示的是匹配a>=0个所以只要含有c就是可以的

而grep '^a*c$'也是正则,表示的是以a开头,且第二个字符匹配a零次或者多次,接下来是c字母的

所以只有aac 和ac 符合条件

所以看这个例子

[root@hadoop-bigdata01 test]# ls a aac abb abbc abc ac b bb c cb [root@hadoop-bigdata01 test]# ls | grep 'a*b' abb abbc abc b bb cb

这里grep 'a*b' 指的可不是含有a和b 而是a重复0次或者多次然后含有b

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13824.html