SHELL的文本處理工具

前言

非常重要,shell面試必備

grep (全局搜索正則表達式)

grep(globle regular expression print)
grep -E = egrep(grep的擴展)
grep 格式:(貪婪模式)
grep 匹配條件 處理文件

  • 過濾root關鍵字:grep root passwd

      [root@rhel7_node1 mnt]# cat passwd
      test:root:test
      test:test:root
      roothahahahahahwestos
      ROOT:test
      westoshelloroot
      hellorootabc
      asdfghjkllkjhgfdsa
      [root@rhel7_node1 mnt]# grep root passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
      westoshelloroot
      hellorootabc
    
  • 以root開頭:grep ^root passwd

      [root@rhel7_node1 mnt]# grep ^root passwd 
      roothahahahahahwestos
    
  • 以root結尾:grep root$ passwd

      [root@rhel7_node1 mnt]# grep root$ passwd 
      test:test:root
      westoshelloroot
    
  • 忽略大小寫:grep -i root passwd

      [root@rhel7_node1 mnt]# grep -i root passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
      ROOT:test
      westoshelloroot
      hellorootabc
    
  • root字符之前不能有字符:grep -E “<root” passwd

      [root@rhel7_node1 mnt]# grep -E "\<root" passwd 
      test:root:test
      test:test:root
      roothahahahahahwestos
    
  • root字符之後不能有字符:grep -E “root>” passwd

      [root@rhel7_node1 mnt]# grep -E "root\>" passwd 
      test:root:test
      test:test:root
      westoshelloroot
    
  • 顯示過濾行以及上面n行和下面n行:grep -數字n

      [root@rhel7_node1 mnt]# grep -2 westoslinux passwd
      mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
      operator:x:11:0:operator:/root:/sbin/nologin
      westoslinux test
      games:x:12:100:games:/usr/games:/sbin/nologin
      ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    
  • 顯示匹配的行所在行號:grep -n

      [root@rhel7_node1 mnt]# grep -n westoslinux passwd
      11:westoslinux test
    
  • 顯示過濾行以及下面幾行:grep -A數字

       [root@rhel7_node1 mnt]# grep -A2 westoslinux passwd
      westoslinux test
      games:x:12:100:games:/usr/games:/sbin/nologin
      ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    
  • 顯示過濾行以及上面幾行:grep -B數字

      [root@rhel7_node1 mnt]# grep -B2 westoslinux passwd
      mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
      operator:x:11:0:operator:/root:/sbin/nologin
      westoslinux test
    
  • 反向過濾:grep -v

grep字符數量匹配規則:

  • 以westos開頭:^westos

      grep -E "^westos" 文件名
    
  • 以westos結尾:westos$

  • w開頭s結尾中間4個任意字符:w…s

  • s結尾前面5個任意字符:…s

  • 字符出現0到任意次:*

  • 1到任意次:+

  • 0到1次:?

  • n次:{n}

  • m到n次:{m,n}

  • 0到n次:{0,n}

  • 0到n次:{,n}

  • 最少m次:{m,}

  • hai字符串出現2次:(hai){2}

練習腳本:
請顯示系統中能被su命令切換的用戶名稱

[root@rhel7_node1 etc]# vim show_loginuser.sh
#!/bin/bash
grep -E "bash$|sh$|tsh$|csh$" /etc/passwd | cut -d : -f 1

測試:

[root@rhel7_node1 etc]# sh show_loginuser.sh 
root
westos
westos1
westos2

sed(stream editor流編輯)

命令格式:
sed 參數 命令 處理對象
sed 參數 -f 處理規則文件 處理對象

對字符的處理

  • 顯示:p
    sed -n 5p westos (顯示第五行)
    sed -n 3,5p westos (顯示3到5行)
    sed -ne “3p;5p” westos (顯示3和5行)
    sed -ne 1,5p westos (1-5行)
    sed -ne ‘5,$p’ westos (5到最後以行)
    sed -n ‘/^#/p’ fstab (顯示以#開頭的行)
  • 刪除:d
    sed 5d westos (刪除第五行)
    sed ‘/^#/d’ fstab (把#開頭的行刪除)
    sed ‘/^UUID/!d’ fstab (除了UUID以外的行都刪除)
    sed -e ‘5,$d’ westos(刪除5行及其之後文件)
  • 添加:a
    sed -e ‘$a hello world’ fstab(最後一行添加)
    sed -e ‘$a hello\nworld’ fstab(添加且換行)
    sed -e ‘/^#/a hello world’ fstab(在以#開頭的後面添加)
  • 替換:c
    sed -e ‘/^#/c hello world’ fstab(以#號開頭的都替換)
    sed ‘5chello world’ westos(將第5行替換)
  • 把符合的行寫到指定文件中:w
    sed ‘/^UUID/w westofile’ westos(把westos中UUID開頭的行寫入westosfile中)
  • 插入:i
    sed ‘5i hello westos’ westos(插入到第五行上面)
  • 整合文件:r
    sed ‘5r haha’ westos(將haha整合到westos文件第5行下面)

sed 字符批量替換

	sed 's/:/###/g' westos(全文範圍把:替換爲###)
	sed 's/:/###/' westos(每行第一個替換)
	sed '1,5s/:/###/g' westos(第一行到第五行)
	sed '1s/:/###/g;5s/:/###/g' westos(第一行和第五行)
	sed '/lp/,/shutdown/s/:/###/g' westos(lp到shutdown之間的)
	sed 's/\//####/g' westos(把/替換掉得轉義這個)
	sed 's@/@####@g' westos(分隔符可以用@表示)
	保存更改內容:
	sed 's@/@####@g' -i westos (把sed處理的內容保存到westos文件中)

練習腳本:
Apache_port.sh
此腳本後接入數字
http的端口就改爲此數字
假設selinux爲關閉狀態

腳本設置:

#!/bin/bash
[ -z "$1" ] && {
        echo Error: not port number Please give port following script
        exit
}
[ -z "`netstat -antlupe | grep $1`" ] || {   
        echo Error:$1 is used by system proto!
        exit
}
[ -e "/etc/httpd/conf/httpd.conf" ] || {
        yum install httpd -y &> /dev/null || {
                echo apache not installed and yum repo is not avaliable
                exit
        }
}
sed "/^Listen/c Listen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd  > /dev/null &&{
        echo "Configure port sucessfully!!"
} ||{
        echo "Error: can't up service!"
}

測試:

[root@rhel7_node1 mnt]# sh  apache_port.sh 2227
Configure port sucessfully!!

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 #第三列
#/etc/passwd文件的第六列沒有home關鍵字並且以bash結尾的行
awk -F : ‘KaTeX parse error: Expected 'EOF', got '&' at position 10: 6!~/home/&̲&/bash/{print}’ /etc/passwd

課後練習
統計如何在系統中能su切換的並且用戶加目錄不在/home下的用戶數量

答案:

awk -F : 'BEGIN{N=0}$6!~/home/&&/bash$|csh$|tcsh$/{print $1;N++}END{print N}' /etc/passwd

後記

練習and補進度
ballball你了!
反向單引號,之間的東西優先執行

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