天天看點

shell.2文本處理工具1.grep2.sed3.awk

文章目錄

  • 1.grep
    • 作用
    • 格式
  • 2.sed
  • 3.awk

1.grep

作用

Linux系統中grep指令是一種強大的文本搜尋工具,它能使用正規表達式搜尋文本,并把匹 配的行列印出來。

grep全稱是Global Regular Expression Print,表示全局正規表達式版本,它的使用權限是所有使用者。

grep家族包括grep、egrep和fgrep。egrep和fgrep的指令隻跟grep有很小不同。egrep是grep的擴充,支援更多的re元字元,

fgrep就是fixed grep或fast grep,

它們把所有的字母都看作單詞,也就是說,正規表達式中的元字元表示回其自身的字面意義,不再特殊。

格式

  • grep -E = egrep

  • grep 格式
  • grep +比對條件+處理檔案

grep root file #過濾root關鍵字
grep ^root file ##以root開頭
grep root$ file ##以root結尾
grep -i root file ##後略大小寫
grep -E "\<root" file ##root字元之前不能有字元
grep -E "root\>" file ##root字元之後不能有字元
grep -數字 ##顯示過濾行以及上面幾行和下面幾行
grep -n ##顯示比對的行所在行号
grep -A ##顯示過濾行以及下面幾行
grep -B ##顯示過濾行以及上面幾行
grep -v ##反向過濾
           
shell.2文本處理工具1.grep2.sed3.awk
shell.2文本處理工具1.grep2.sed3.awk
  • grep字元數量比對規則
^myr 以myr開有
myr$ 以myr結尾
w…s w開頭s結尾中間4個任意字元
…s s結尾前面3個任意字元
字元出現0到任一次
? 0到1次
1到任一次
{n} n次
{m,n} m到n次
{0,n} 0-n次
{,n} 0-n次
{m,} 最少m次
(myr){2} lee字元串出現2次
shell.2文本處理工具1.grep2.sed3.awk

練習腳本:

請顯示系統中能被su指令切換的使用者名稱

shell.2文本處理工具1.grep2.sed3.awk
shell.2文本處理工具1.grep2.sed3.awk

2.sed

  • 指令格式:
  • sed 參數 指令 處理對象

  • sed 參數 處理對象 -f 處理規則檔案

  • 對字元的處理:
參數 作用
p 顯示
d 删除
a 添加
c 替換
w 把符合的行寫到指定檔案中
i 插入
r 整合
  • p ##顯示
sed -n 5p file ##顯示第五行
sed -n 3,5p file ##顯示3到5行
sed -ne "3p;5p" file ##顯示3和5行
sed -ne 1,5p file ##1-5行
sed -ne '5,$p' file ##5到最後以行
sed -n '/^#/p' file ##顯示以#開頭的行
sed -n '/#$/p' file##顯示以#結尾的行


           
shell.2文本處理工具1.grep2.sed3.awk
  • d ##删除
sed 5d file ##删除第五行
sed '/^#/d' file ##把#開頭的行删除
sed '/^#/!d' file ##除了#開頭以外的行都删除
sed -e '5,$d' file##删除第五行以後的

           
  • a ##添加
sed -e '$a hello world' file##在最後一行添加hello world
sed -e '$a hello\nworld' file
sed -e '/^#/a hello world' file##在以#開頭的行後添加一行hello world

           
  • c ##替換
sed -e '/^#/c hello world' fstab##把以#開頭的行替換為hello world
sed '5c hello world' westos##把第五行替換為hello world
           
  • w ##把符合的行寫到指定檔案中
  • i ##插入
  • r ##整合檔案
sed '5r haha' file
           
  • sed 字元替換
sed 字元替換
sed 's/:/###/g' file #把全部冒号替換為###
sed 's/:/###/' file #把每行的第一個冒号替換成###
sed 's/:/###/2' file #把每行的第2個冒号替換成###
sed 's/:/###/g' file 
sed '1,5s/:/###/g' file #把第一行和第五行的冒号替換成###
sed '1s/:/###/g' file #把第一行的冒号替換成###
sed '1s/:/###/g;5s/:/###/g' file #把第一行和第五行的冒号替換成###
sed '/lp/,/shutdown/s/:/###/g' file ## 把lp和shutdown之間得冒号全部替換為###
sed 's/\//####/g' file ##把/替換為###,\轉義字元
sed 's@/@####@g' file 
sed 's@/@####@g' -i file 把sed處理的内容儲存到file檔案中
           
  • @和/均可作為分隔符
  • 練習腳本 :

    Apache_port.sh 此腳本後介入數字,http的端口就改為此數字,假設selinux為關閉狀态。

    shell.2文本處理工具1.grep2.sed3.awk
    shell.2文本處理工具1.grep2.sed3.awk
    shell.2文本處理工具1.grep2.sed3.awk

3.awk

  • awk -F 分隔符 BEGIN{}{}END{} FILENAME

    不加-F,分隔符預設為空格
NR 行數
NF 列數
FILENAME 檔案名稱本身
myr myr變量值
“myr” myr字元串
/bash$/ 條件
/條件1︱條件2/ 條件1或者條件2
/條件1/‖/條件2/ 條件1或者條件2
/條件1/&&/條件2/ 條件1并且條件2
$0 所有的列
$1 第一列
$2 第二列
$3 第三列
  • awk -F : '$6!~/home/&&/bash$/{print}' /etc/passwd

    /etc/passwd檔案的第六列沒有home關鍵字并且以bash結尾的行
  • awk -F: '/root/' /etc/passwd

    搜尋/etc/passwd有root關鍵字的所有行
    shell.2文本處理工具1.grep2.sed3.awk
  • 小練習:

    統計在系統中能su切換的并且使用者家目錄不在/home下的使用者數量

    shell.2文本處理工具1.grep2.sed3.awk
    shell.2文本處理工具1.grep2.sed3.awk

繼續閱讀