1.grep
gerp :[Globally search a Regular Expression and Print]
grep -E = egrep
grep格式
**基本格式:grep 比對條件 處理檔案**
grep root passwd(file,/etc/passwd) :過濾root關鍵字
grep -i root passwd :忽略大小寫
grep -E "\<root" passwd :root字元之前不能有字元
grep -E "root\>" passwd :root字元之後不能有字元
grep -數字 :顯示過濾行以及上面幾行和下面幾行
grep -n :顯示比對的行所在行号
grep -A :顯示過濾行以及下面幾行
grep -B :顯示過濾行以及上面幾行
grep -v :反向過濾
**grep字元數量比對規則**
^westos :以westos開頭
westos$ :以westos結尾
w....s :w開頭s結尾中間4個任意字元
.....s :s結尾前面5個任意字元
* :字元出現任意
? :字元出現0到1次
+ :字元出現1到任意次
{n} :字元出現n次
{m,n} :字元出現m到n次
{0,n} :字元出現0到n次
{,n} :字元出現0到n次
{m,} :字元出現最少m次
(lee){2} :lee字元出現2次
2.sed
sed指令格式
sed 參數 指令 處理對象
sed 參數 處理對象 -f 處理規則檔案
**p** 顯示
sed -n 5p westos :顯示第五行
sed -n 3,5p westos :顯示3到5行
sed -ne "3p;5p" westos :顯示3和5行
sed -ne '5,$p' westos :顯示第5行到最後行
sed -n '/^#/p' fstab :顯示以#号開頭的行
**d** 删除
sed 5d westos :删除第五行
sed '/^#/d' fstab(etc/fstab:file):把#開頭的行删除
sed '/^UUID/!d' fstab :除了UUID以外的行都删除
**a** 添加
sed -e '$a hello world' fstab
sed -e '$a hello\world' fstab
sed -e '/^#/a hello world' fstab
**c** 替換
sed -e '/^#/c hello world' fstab
sed '5chello world' westos
**w** 把符合的行寫到指定檔案中
sed '/^UUID/w westofile' westos :把westos中UUID開頭的行寫入westosfile中
**i** 插入
sed '5ihello westos' westos
**r** 整合檔案
sed '5r haha' westos
**sed 字元替換**
sed 's/:/###/g' westos
sed '1,5s/:/###/g' westos
sed '1s/:/###/g' westos
sed '1s/:/###/g;5s/:/###/g' westos
sed '/lp/,/shutdown/s/:/###/g' westos
sed 's/\//####/g' westos
sed 's@/@####@g' westos
sed 's@/@####@g' -i westos :把sed處理的内容儲存到westos檔案中
3.awk(報告生成器)
格式:awk -F 分隔符 BEGIN{}{}END{} FILENAME
NR :行數 NF:列數 FILENAME:檔案名稱
例如 westos :westos變量值 “westos”:westos字元串
條件表示:/bash$/
/條件1|條件2/ :條件1或者條件2
/條件1/||/條件2/ :條件1或者條件2
/條件1/&&/條件2/ :條件1并且條件2
列數表示:
$0 :所有的列
$1 :第一列
$2 :第二列
$3 :第三列
實驗
實驗1
請顯示系統中能被su指令切換的使用者名稱
實驗2
Apache_port.sh:此腳本接入數字,http的端口就改為此數字
假設selinux為關閉狀态
例如:sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口會被修改為8080
實驗3
統計在系統中能su切換的并且使用者加目錄不在/home下的使用者數量