liunx之通配符&正则表达式

LIUNX之通配符

  1. 单引号双引号不加引号区别反引号
  2. ''单引号所见即所得 
  3. “”双引号里面的特殊符号会被解析运行
  4. $ LANG $()`` 
  5. 不加引号和双引号类似支持通配符{} * 
  6. 通配符:方便找出文件 

``$()优先执行先执行里面的命令把结果留下

[root@oldboy56-201 ~]# which mkdir #查询mkdir目录在哪里
/usr/bin/dir
[root@oldboy56-201 ~]# ls -l `which mkdir`  #先执行`反义符里面`
-rwxr-xr-x. 1 root root 79760 Apr 11  2018 /usr/bin/mkdir
[root@oldboy56-201 ~]# ls -l $(which mkdir)
-rwxr-xr-x. 1 root root 79760 Apr 11  2018 /usr/bin/mkdir

liunx之正则表达式

基本正则表达式第一波

  • ^ word:以字开头
grep '^m' oldboy.txt #查找以m开头的文字
my blog is http://oldboy.blog.51cto.com 
my qq is 49000448
my god ,i am not oldbey,but OLDBOY!
  • 字$:以字结尾的
[root@oldboy56-201 oldboy]# grep "m$" oldboy.txt #以m结尾的文件
ldmam
  • ^ $:空行
[root@oldboy56-201 oldboy]# grep '^$' oldboy.txt #'^$' :空行


[root@oldboy56-201 oldboy]# grep -n  '^$' oldboy.txt 
6:
11:
[root@oldboy56-201 oldboy]# cat -n oldboy.txt
     1	ldmam
     2	this is boy
     3	this is boy
     4	I am oldboy teacher!
     5	I teach linux.
     6	
     7	I like badminton ball ,billiard ball and chinese chess!
     8	my blog is http://oldboy.blog.51cto.com 
     9	our size is http://blog.oldboyedu.com 
    10	my qq is 49000448
    11	
    12	not 4900000448.
    13	my god ,i am not oldbey,but OLDBOY!
    14	job jb joooob

基本正则之第二波

  • 。代表和代表一个字符(对比下面的代码出现缺少6和11行的空行。相当于有一个字符空格的则被的过滤了)
[root@oldboy56-201 oldboy]# cat -n oldboy.txt
     1	ldmam
     2	this is boy
     3	this is boy
     4	I am oldboy teacher!
     5	I teach linux.
     6	
     7	I like badminton ball ,billiard ball and chinese chess!
     8	my blog is http://oldboy.blog.51cto.com 
     9	our size is http://blog.oldboyedu.com 
    10	my qq is 49000448
    11	
    12	not 4900000448.
    13	my god ,i am not oldbey,but OLDBOY!
    14	job jb joooob
    [root@oldboy56-201 oldboy]# grep -n  "." oldboy.txt
    1:ldmam
    2:this is boy
    3:this is boy
    4:I am oldboy teacher!
    5:I teach linux.
    7:I like badminton ball ,billiard ball and chinese chess!
    8:my blog is http://oldboy.blog.51cto.com 
    9:our size is http://blog.oldboyedu.com 
    10:my qq is 49000448
    12:not 4900000448.
    13:my god ,i am not oldbey,but OLDBOY!
    14:job jb joooob
  • 转义转的将将将的文件)
[root@oldboy56-201 oldboy]# grep -n ".$" oldboy.txt
1:ldmam
2:this is boy
3:this is boy
4:I am oldboy teacher!
5:I teach linux.
7:I like badminton ball ,billiard ball and chinese chess!
8:my blog is http://oldboy.blog.51cto.com 
9:our size is http://blog.oldboyedu.com 
10:my qq is 49000448
12:not 4900000448.
13:my god ,i am not oldbey,but OLDBOY!
14:job jb joooob
[root@oldboy56-201 oldboy]# grep -n "\.$" oldboy.txt
5:I teach linux.
12:not 4900000448.
  • *前面一个字符连续出现了0次或0次以上
[root@oldboy56-201 oldboy]# grep "0*" oldboy.txt
ldmam
this is boy
this is boy
I am oldboy teacher!
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448

not 4900000448.
my god ,i am not oldbey,but OLDBOY!
job jb joooob
  • 。*匹配所有字符延伸^。*以所有字符开头,以所有字符结尾(如下图所有的字符都输出了。*匹配了所有字符)
[root@oldboy56-201 oldboy]# grep ".*" oldboy.txt
ldmam
this is boy
this is boy
I am oldboy teacher!
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448

not 4900000448.
my god ,i am not oldbey,but OLDBOY!
job jb joooob

 

基础正则表达式第三部

  • [abc]相当于是一个符号(每次匹配1个字符)找出包含a或b或c(如下例子匹配所有小写字母)
[root@oldboy56-201 oldboy]# grep "[a-z]" oldboy.txt
ldmam
this is boy
this is boy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
job jb joooob
  • [^ abc]匹配不包含^后任意一个字符的内容,中括号中^为取反,注意和中括号^开头区分(匹配所有大写字母)
  • [root@oldboy56-201 oldboy]# grep "[^a-z]" oldboy.txt
    this is boy
    this is boy
    I am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com 
    our size is http://blog.oldboyedu.com 
    my qq is 49000448
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY!
    job jb joooob
    

     

扩展正则表达式

  •  0 {nm}前一个字符至少连续出现n次,连续出现m次

  • [root@oldboy56-201 oldboy]# egrep "0{1,4}" oldboy.txt
    my qq is 49000448
    not 4900000448.
    

     

  • 0 {n}前面一个字符连续出现n次

    [root@oldboy56-201 oldboy]# egrep [0-9]{17}[0-9X] id.txt
    金 211324198705244720
    万 500224197105168312
    吕 211282199209113038
    孔 150000198309176071
    邹 371001197412221284
    贺 130185200011215926
    杜 362522198711278101
    向 14052219961008852X
    

     

posted @ 2018-12-23 22:06  blackBrock  阅读(151)  评论(0编辑  收藏  举报