天天看點

linux通配符與基礎正則

1.特殊符号:

' '     單引号   

       單引号的内容     寫什麼就是什麼,不會被當成特殊字元.

       單引号執行個體:

      [root@oldboyedu-sh01 ~]# echo  '$123'

      $123

" "    雙引号

       雙引号裡面的内容會被解析

       雙引号執行個體:

      [root@oldboyedu-sh01 ~]# echo  "$123"

      23

            ## $1 被當成了變量,而在單引号中不會出現這種問題

` `  (鍵盤中 1 的右邊),稱為  反引号 

     反引号會讓引号内的内容優先執行

     反引号 執行個體:

     [root@oldboyedu-sh01 ~]# ls -l   `ls`

    -rw-r--r--. 1 root root     0 Apr  9 11:14 2

    -rw-------. 1 root root  1162 Mar 22 12:05 anaconda-ks.cfg

    -rw-r--r--. 1 root root 21736 Mar 22 12:05 install.log

    -rw-r--r--. 1 root root  5890 Mar 22 12:04 install.log.syslog

    -rw-r--r--. 1 root root   242 Apr  9 10:02 oldboy.txt

!    感歎号

    find排除

    find  chenleilei/ -type f  ! -name "2" -name "*"

      #排除多個請看下面的執行個體

    感歎号為取反的意思  (find 指令中 ! 隻能排除一個檔案或者檔案夾,但可以在後面接

    egrep -v  來排除多個)

    感歎号執行個體:

    [root@oldboyedu-sh01 ~]# find  chenleilei/ -type f  -name "*" | egrep -v "3|2|1"

    chenleilei/7

   chenleilei/8

    chenleilei/9

   chenleilei/5

   chenleilei/4

   chenleilei/6

 ^符号  以XX開頭

[root@chenleilei ~]# grep  ^7 3.txt

743q9hf9g87rfg23

& &    并且   (一個指令執行成功後再執行下一個指令)

例子:

ifdown eth0 && ifup eth0

[]    代表的是中括号中的任意一個

      例子:[root@chenleilei

~]# ls [1,2,3].txt 

              1.txt 2.txt 3.txt 

*     任意一個或者多個字元,或比對所有

       例子:   [root@chenleilei ~]# ls *.txt

                  1.txt

 2.txt 3.txt

?     任意一個單個字元

      例子:[root@chenleilei ~]# ls ?.txt 

>      覆寫輸出重定向

        echo  '123'  >1.txt 

>>   追加輸出重定向

        echo  '123'  >1.txt 

<   輸入重定向

     cat >1.txt < eof

    1111111

     eof        輸入重定向

<<   追加輸入重定向

        cat >1.txt<<eof

        1111111

        eof

[-]  代表一個範圍  如  [a-zA-z]

     例子: [0-9]  代表括号内得0到9得任意字元會被比對

 [^]   非得意思如 [^abc]  -- 隻要不是abc其他都行  排除得意思

{ }  生成序列   

    例子:  touch  {1..10}.txt

.      目前目錄

..     上一級目錄

基礎正則:

{..}    查找或删除檔案時指定多個檔案類型   (查詢)

        如: ls {*.txt,*.jpg}

          [root@oldboyedu-sh01 chenleilei]# ls {*.txt,*.jpg}

          1.jpg  1.txt

{..}     echo 總 它是指定 一個數列 和數列間得間隔  (生成數列)

       [root@oldboyedu-sh01 chenleilei]# echo  {1..10..3}

       1 4 7 10

{..}     同時1.{,.bak}  也可以進行備份            (備份)

       [root@oldboyedu-sh01 chenleilei]# cp 1.txt{,.bak}

       [root@oldboyedu-sh01 chenleilei]# ls

         1  10  1.jpg  1.txt  1.txt.bak 2  3  4  5  6  7  8  9

^    以什麼開頭

      [root@oldboyedu-sh01 chenleilei]# ls 99*.txt

      9992ef9.txt  9999999999.txt

^$    表示為 空行

     [root@oldboyedu-sh01 ~]# grep --color=auto -n '^$' oldboy.txt

     3:

     8:

擴充執行個體:

統計一個檔案中 每個字元使用得次數

[root@oldboyedu-sh01 ~]# grep -o '[0-9]' oldboy.txt |sort |uniq -c

[root@oldboyedu-sh01 ~]# grep -o '[0-9]' oldboy.txt | sort | uniq -c

      8 0

      1 1

      6 4

      1 5

      2 8

      2 9

linux通配符與基礎正則

繼續閱讀