shell 特殊符號介紹

8.10 shell特殊符號&cut命令

特殊符號

  • “*” 代表零個或多個字符

  • “?” 代表一個字符

  • “#” 註釋符號

  • “\” 脫意符號

  • “|” 管道符

  • “$” 該符號與“!”合用“!$”表示上一條命令中的最後一個變量

  • “;” 分隔符,在一行中運行兩個及兩個以上的命令時使用

  • “~” 用戶的家目錄(root用戶“/root”,普通用戶“/home/username”)

  • “&” 如果想把一條命令直接放到後臺運行的話,可以在命令行加上這個符號(通常用於運行時間非常長的命令)

  • “[]” 中括號中間爲字符組合,代表中間字符中的任意一個。

cut命令

cut命令用來顯示行中的指定部分,刪除文件中指定字段。cut經常用來顯示文件的內容,類似於下的type命令。
說明:該命令有兩項功能,其一是用來顯示文件的內容,它依次讀取由參數file所指明的文件,將它們的內容輸出到標準輸出上;其二是連接兩個或多個文件,如cut fl f2 > f3將把文件fl和幾的內容合併起來,然後通過輸出重定向符“>”的作用,將它們放入文件f3中。

語法: cut -d ‘分隔符’ [-cf] n [filename] (這裏n是正整數)
-d:指定分隔符號
-f:指定第幾段
-c:後面只有一個數字表示截取第幾個字符;後面跟一個數字區域,表示截取從幾到幾(該選項不和d,f共同使用)

[root@adai003 tmp]# cut -c1 1.txt |head -n2r
b
[root@adai003 tmp]# cut -c1,3 1.txt |head -n2ro
bn
[root@adai003 tmp]# cut -f1,3 -d ':' 1.txt |head -n2root:0bin:1[root@adai003 tmp]# cut -f1-3 -d ':' 1.txt |head -n2root:x:0bin:x:1

8.11 sort、wc、uniq命令

sort命令

sort命令是在Linux裏非常有用,它將文件進行排序,並將排序結果標準輸出。sort命令既可以從特定的文件,也可以從stdin中獲取輸入。

語法: sort [-t 分隔符] [options] [filename]
Options:
-t:指定分隔符
-n:使用純數字排序(系統默認所有字母爲0)
-r:反向排序
-u:=unique 去重複
-kn1,n2:由n1區間排序到n2區間,可以只寫-kn1,即對n1字段排序(n1 < n2)
sort不加任何選項,則從首字符向後,依次以ASCⅡ碼值進行比較,最後將它們按升續輸出。

[root@adai003 tmp]# head -n3 1.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin[root@adai003 tmp]# head -n3 1.txt |sortbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinroot:x:0:0:root:/root:/bin/bash

wc命令

wc命令用來計算數字。利用wc指令我們可以計算文件的Byte數、字數或是列數。

語法: wc [options] [filename]
Options:
-l:=line 統計行數
-m:=member 統計字符數
-w:=Word 統計詞數

[root@adai003 tmp]# wc -l !$wc -l 2.txt2 2.txt
[root@adai003 tmp]# wc -m 2.txt10 2.txt
[root@adai003 tmp]# cat !$cat 2.txt1234qwer
[root@adai003 tmp]# cat -A 2.txt1234$qwer$[root@adai003 tmp]# wc -w 2.txt4 2.txt
[root@adai003 tmp]# cat 2.txt1234 456 789,10qwer

說明: wc -m會統計文件內所有字符,包括隱藏的換行符“&”;wc -w是以空格作爲分隔符進行詞組統計的。

uniq命令(unique)

uniq命令用於報告或忽略文件中的重複行,一般與sort命令結合使用(即:去重複)。

語法: uniq [options] [filename]
Options:
-c:=count 在每列旁邊顯示該行重複出現的次數

[root@adai003 tmp]# cat !$cat 2.txt1234456 789,101234qwer456[root@adai003 tmp]# uniq -c 2.txt
      1 1234
      1 456 789,10
      1 1234
      1 qwer      1 456[root@adai003 tmp]# sort 2.txt |uniq -c
      2 1234
      1 456
      1 456 789,10
      1 qwer

說明: 直接使用uniq命令,2.txt內容顯示並沒有變化,使用sort排序後再用uniq命令,重複行被合併,即:在對文件進行去重之前需要先進行排序!

8.12 tee、tr、split命令

tee命令

tee命令用於將數據重定向到文件,會刪除文件內原有內容,與“>”不同的是,tee會把定向的文件內容顯示出來。

語法: tee [options] [filename]
Options:
-a:向文件中重定向時使用追加模式(=“>>”)

[root@adai003 tmp]# cat 3.txt00000000000[root@adai003 tmp]# sort 2.txt |uniq -c |tee 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
[root@adai003 tmp]# cat 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
      [root@adai003 tmp]# sort 2.txt |uniq -c |tee -a 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
[root@adai003 tmp]# cat 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer      2 1234
      1 456
      1 456 789,10
      1 qwer

tr命令

tr命令可以對來自標準輸入的字符進行替換、壓縮和刪除,它可以將一個字符變成另一個字符,也可以將一組字符變成另一組字符。

語法: tr [源字符] [目標字符]

[root@adai003 tmp]# echo "adailinux" |tr 'a' 'A'AdAilinux   替換一個字符
[root@adai003 tmp]# echo "adailinux" |tr '[al]' '[AL]'AdAiLinux   替換多個字符
[root@adai003 tmp]# echo "adailinux" |tr '[a-z]' '[A-Z]'ADAILINUX

split命令

split命令可以將一個大文件分割成很多個小文件,有時需要將文件分割成更小的片段,比如爲提高可讀性,生成日誌等。

語法: split [options] [filename]
-b:指定每一輸出檔案的大小,默認單位爲 byte,可自定義單位,如 split -b 100M filename
-l:指定每一個輸出檔案的行數多少
eg1: 指定大小

[root@adai003 tmp]# split -b 100 1.txt[root@adai003 tmp]# lsxaaxabxacxad[root@adai003 tmp]# rm -rf x*[root@adai003 tmp]# split -b 100 1.txt adai.  
可以指定文件前綴![root@adai003 tmp]# lsadai.aa adai.abadai.acadai.ad

eg2: 指定行數

[root@adai003 tmp]# wc -l 1.txt20 1.txt
[root@adai003 tmp]# split -l 5 1.txt[root@adai003 tmp]# lsxaa
xab
xac
xad
[root@adai003 tmp]# wc -l x*
  5 xaa  5 xab  5 xac  5 xad 20 總用量

8.13 shell特殊符號(下)

命令連接符: “||”、“&&”、“;”

  • command1 ; command2 : 不管command1是否執行成功都會執行command2

  • command1 && command2 : 只有command1執行成功後纔會執行command2

  • command1 || command2 : 表示command1執行成功後,command2不執行,否則執行command2


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章