玩轉Linux命令之三劍客

來源:互聯網
上載者:User

玩轉Linux命令之三劍客
1 grep命令

功能說明
grep(Global Regular Expression Print)是一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹配的行列印出來。其格式如下
grep[選項]pattern file

grep命令的選項及其說明如下

選項 說明
-v 過濾指定字串內容的行
-i 不區分大小寫
-E 過濾多個字串
-o 輸出多個精確匹配的字元,而不是整行

使用案例

[root@tony data]# vim teach.txt #建立測試資料(編輯測試資料內容)Program languageJavaCC++PHPPythonRubyOSWindowswindowsLinuxMacUnixAndroidIOS

過濾掉指定的字串(“windows”)匹配的行並忽略大小寫後將結果輸出

[root@tony data]# grep -vi 'windows' teach.txt #忽略大小寫並過濾包含Windows字串的行Program languageJavaCC++PHPPythonRubyOSLinuxMacUnixAndroidIOS

過濾掉多個匹配字串內容(‘Android|IOS’)的行並將結果輸出

[root@tony data]# grep -vE 'Android|IOS' teach.txt #過濾Android和IOS字串對應的行Program languageJavaCC++PHPPythonRubyOSWindowswindowsLinuxMacUnix

尋找指定的進程

[guanglei@tony data]$ ps -ef|grep ssh #尋找SSH遠程加密串連的進程root 1403 1 0 09:54 ? 00:00:00 /usr/sbin/sshdroot 1509 1403 0 10:00 ? 00:00:00 sshd: guanglei [priv]guanglei 1511 1509 0 10:00 ? 00:00:00 sshd: guanglei@pts/1guanglei 1654 1512 0 10:21 pts/1 00:00:00 grep ssh
2 sed命令

功能說明
sed主要用來編輯檔案,簡化對檔案的反覆操作,編寫轉換程式等,擅長取行。其格式如下
sed [選項] [n1,[n2]][function],n1,n2表示選擇行數的區間

選項 說明
-n 取消預設輸出
-i 改變檔案內容

fuction:
d:刪除
i:插入
p:列印
s:替換

使用案例

尋找匹配字串內容的行

[root@tony data]# sed -n '/J/p' teach.txt #匹配指定字串的行 Java

刪除匹配字串內容的行

[root@tony data]# sed -i '/Python/d' teach.txt [root@tony data]# cat teach.txt Program languageJavaCC++PHPRubyOSWindowswindowsLinuxMacUnixAndroidIOS[root@tony data]# 

實現檔案內容的替換

[root@tony data]# sed -i 's#Android#android6.0#g' teach.txt #將Android替換成Android6.0[root@tony data]# cat teach.txt Program languageJavaCC++PHPRubyOSWindowswindowsLinuxMacUnixandroid6.0IOS

實現指定檔案內容的尋找並替換

[root@tony data]# mkdir -p grandparent/parent/son #建立三級目錄[root@tony data]# tree #查看分類樹結構.├── grandparent│ ├── parent│ │ ├── son│ │ │ └── test.txt│ │ └── test.txt│ └── test.txt└── teach.txt[root@tony data]# echo "This is Linux Operator System" >grandparent/test.txt #建立測試資料[root@tony data]# echo "This is Linux Operator System" >grandparent/parent/test.txt[root@tony data]# echo "This is Linux Operator System" >grandparent/parent/son/test.txt[root@tony data]# find ./ -type f -name "test.txt"|xargs sed -i 's#Linux#Unix#g' #將匹配的檔案內容字串轉換為Unix[root@tony data]# cat grandparent/test.txt #查看修改結果This is Unix Operator System

擷取指定區間行數的內容

[root@tony data]# sed -n '2,5p' teach.txt Program languageJavaCC++
3 awk命令

awk是一種程式設計語言,允許建立簡短的程式,這些程式讀取輸入檔案,為資料排序,處理資料,對輸入執行計算以及產生報表。其格式如下

awk ‘{pattern +action}’ {fileNames}

選項 說明
-F 指定分割的內容

內建變數
NR: 表示行號

使用案例

使用awk實現擷取指定區間的內容

[root@tony data]# awk '{if(NR<5&&NR>=2)printf $1 "\n"}' teach.txt ProgramJavaC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.