Shell正則運算式(二)
- sed工具概述
- 輸入符號條件的文本(p)
- 洗掉符合條件的檔案(d)
- 替換符合條件的文本(s)
- 遷移符合條件的文本
- 使用sed直接操作檔案
sed工具概述
sed是一種流編輯器,它是文本處理中非常中的工具,能夠完美的配合正則運算式使用,功能不同凡響,處理時,把當前處理的行存盤在臨時緩沖區中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往螢屏,接著處理下一行,這樣不斷重復,直到檔案末尾,檔案內容并沒有改變,除非你使用重定向存盤輸出,Sed主要用來自動編輯一個或多個檔案;簡化對檔案的反復操作;撰寫轉換程式等,
1.作業流程
讀取—執行—顯示
常見用法
sed [選項] '操作' 引數
sed [選項] -f scriptfile 引數
1).常見選項
-e <script>或--expression=<script>:以選項中的指定的script來處理輸入的文本檔案;-f<script檔案>或--file=<script檔案>:以選項中指定的script檔案來處理輸入的文本檔案;-h或--help:顯示幫助;-n或--quiet或——silent:僅顯示script處理后的結果;-V或--version:顯示版本資訊;-i 直接編輯文本檔案;
2).常見操作a: 在當前行下面插入檔案;i: 在當前行上面插入文本;c: 把選定的行改為新的文本;d: 洗掉,洗掉選擇的行;s: 替換指定字符;p: 列印模板塊的行;g: 表示行內全面替換;w:表示把行寫入一個檔案;G:追加到當前模板塊文本的后面;=:列印當前行號碼;
2.sed用法示例
創建測驗檔案
[root@CentOS-3 ~]# vim test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
輸入符號條件的文本(p)
輸入所有內容,等同于cat test.txt
[root@CentOS-3 ~]# sed -n 'p' test.txt

輸入第3行
[root@CentOS-3 ~]# sed -n '3p' test.txt

輸出第3-5行
[root@CentOS-3 ~]# sed -n '3p' test.txt

輸出所有奇數行(n—表示讀入下一行)
[root@CentOS-3 ~]# sed -n 'p;n' test.txt

輸出所有偶數行
[root@CentOS-3 ~]# sed -n 'n;p' test.txt

輸出1-5奇數行
[root@CentOS-3 ~]# sed -n '1,5{p;n}' test.txt

輸出第10行至檔案尾的偶數行(其實輸出的是10,11,13,15……等行)
[root@CentOS-3 ~]# sed -n '10,${n;p}' test.txt

輸出包含the的行
[root@CentOS-3 ~]# sed -n '/the/p' test.txt

輸出第4行至第1個包含the的行
[root@CentOS-3 ~]# sed -n '4,/the/p' test.txt

輸出包含the的所在行的行號(= 用來輸出行號)
[root@CentOS-3 ~]# sed -n '/the/=' test.txt

輸出以PI開頭的行
[root@CentOS-3 ~]# sed -n '/^PI/p' test.txt

輸出以數字結尾的行
[root@CentOS-3 ~]# sed -n '/[0-9]$/p' test.txt

輸出包含單詞wood的行 < ,>表示單詞邊界
[root@CentOS-3 ~]# sed -n '/\<wood\>/p' test.txt

洗掉符合條件的檔案(d)
nl—計算檔案的行數
洗掉第3行
[root@CentOS-3 ~]# nl test.txt | sed '3d'

洗掉3-5行
[root@CentOS-3 ~]# nl test.txt | sed '3,5d'

洗掉包含cross的行
[root@CentOS-3 ~]# nl test.txt | sed '/cross/d'

洗掉不包含cross的行
[root@CentOS-3 ~]# nl test.txt | sed '/cross/!d'

洗掉開頭為小寫字母的行
[root@CentOS-3 ~]# sed '/^[a-z]/d' test.txt

洗掉以‘.'結尾的行
[root@CentOS-3 ~]# sed '/\.$/d' test.txt

洗掉空行
[root@CentOS-3 ~]# sed '/^$/d' test.txt

洗掉重復的空行
[root@CentOS-3 ~]# sed '/^$/{n;/^$/d}' test.txt
等同于
[root@CentOS-3 ~]# cat -s test.txt

替換符合條件的文本(s)
將每行中的第1個the替換為THE
[root@CentOS-3 ~]# sed 's/the/THE/' test.txt

將每行中的第2個l替換為L
[root@CentOS-3 ~]# sed 's/l/L/2' test.txt

將檔案中所有的the替換為THE
[root@CentOS-3 ~]# sed 's/the/THE/g' test.txt

將檔案中所有的o洗掉(替換為空串)
[root@CentOS-3 ~]# sed 's/o//g' test.txt

每行開始添加#字符
[root@CentOS-3 ~]# sed 's/^/#/' test.txt

在包含the的每行行首添加#字符
[root@CentOS-3 ~]# sed '/the/s/^/#/' test.txt

在每行末尾添加EOF字符
[root@CentOS-3 ~]# sed 's/$/EOF/' test.txt

將3-5行所有的the替換為THE
[root@CentOS-3 ~]# sed '3,5s/the/THE/g' test.txt

將包含the的行中的o替換為O
[root@CentOS-3 ~]# sed '/the/s/o/O/g' test.txt

遷移符合條件的文本
- H:復制到剪貼板;
- g,G:將剪貼板中的資料覆寫\追加到指定行;
- w:保存為檔案;
- r:讀取指定檔案;
- a:追加指定內容;
將包含the的行遷移到行尾(;用于多個操作)
H復制到剪貼板---d洗掉---$G追加到行尾
[root@CentOS-3 ~]# sed '/the/{H;d};$G' test.txt

將1-5行遷移到17行后
[root@CentOS-3 ~]# sed '1,5{H;d};17G' test.txt

將包含the的行另存為新檔案
[root@CentOS-3 ~]# sed '/the/w out.file' test.txt
[root@CentOS-3 ~]# ls
anaconda-ks.cfg out.file test.txt
[root@CentOS-3 ~]# cat out.file

在包含the每行后添加檔案hostname內容
[root@CentOS-3 ~]# sed '/the/r /etc/hostname' test.txt

在第3行后插入新行,內容為New
[root@CentOS-3 ~]# sed '3aNew' test.txt

在包含the的每行后插入新行
[root@CentOS-3 ~]# sed '/the/aNew' test.txt

在第3行后插入多行(\n 換行符)
[root@CentOS-3 ~]# sed '3aNew1\nNew2' test.txt

5).使用腳本編輯檔案
使用sed腳本,將多個編輯指令存放到檔案中(每行一條編輯指令),通過 -f 呼叫
例:將1-5行遷移到17行后
可以使用下列腳本實作
[root@CentOS-3 ~]# vim opt.list
1,5H
1,5d
17G
[root@CentOS-3 ~]# sed -f opt.list test.txt

使用sed直接操作檔案
準備作業:安裝vsftpd服務

編輯腳本:調整vsftpd服務配置:禁止匿名用戶,但允許本地用戶(也允許寫入)
[root@CentOS-3 ~]# vim local_only_ftp.sh
#!/bin/bash
# 指定組態檔樣本路徑、組態檔路徑
sample="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf"
config="/etc/vsftpd/vsftpd.conf"
#備份原來的組態檔:檢測備份檔案是否存在,不存在進行備份
[ ! -e "${config}.bak" ] && cp $config ${config}.bak
# 將樣例檔案中的以anonymous_enable開頭的行中yes替換為no,并覆寫ftp組態檔
sed -e '/^anonymous_enable/s/YES/NO/g' $sample > $config
#查看是否有包含listen的行,否則在檔案末尾添加此行
grep "listen" $config || sed -i '$alisten=YES' $config
# 重啟服務并開機自啟動;查看服務埠號
systemctl restart vsftpd
systemctl enable vsftpd
nsten的行,否則在檔案末尾添加此行
運行腳本進行查看

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/65967.html
標籤:其他
