Linux sed的一些簡單用法

sed命令:

sed(意爲流編輯器,源自英語“stream editor”的縮寫)是一個使用簡單緊湊的編程語言來解析和轉換文本Unix實用程序。
sed由貝爾實驗室的Lee E. McMahon於1973年至1974年開發,並且現在大多數操作系統都可以使用。sed基於交互式編輯器ed(“editor”,1971)和早期qed(“quick editor”,1965-66)的腳本功能。
sed是最早支持正則表達式的工具之一,至今仍然用於文本處理,特別是用於替換命令。用於純文本字符串操作和“流編輯”的常用工具還有AWK和Perl 

下面說一些sed的常用的參數:
        首先交代一下我的測試文本:

[oracle@rhel scripts]$ more test1231.log 
ORA-01555
ORA-01556
ORA-01557
ORA-01558fortest
ORA-01559ORA-01560
/etc/passwd

 

1. sed '[address]s/str1/str2/flag'
        其中:
            address用來指定操作區間:
                以數字形式指定區間:
                    2------代表只修改第2行

[oracle@rhel scripts]$ sed -n '2s/ORA/ora/p' test1231.log   
ora-01556

                    2,$----代表從第2行開始遍歷

[oracle@rhel scripts]$ sed -n '2,$s/ORA/ora/p' test1231.log
ora-01556
ora-01557
ora-01558fortest
ora-01559ORA-01560

                    2,5----代表從第2行到第9行

[oracle@rhel scripts]$ sed -n '2,5s/ORA/ora/2p' test1231.log 
ORA-01559ora-01560

                以文本模式指定區間:
                    /pattern/command 例如:

[oracle@rhel scripts]$ sed -n '/ORA-01559/s/ORA/ora/p' test1231.log 
ora-01559ORA-01560

            flag用來進行標記:
                n-----爲1~512之間的數字,表示指定要替換的字符串要出現第幾次,才進行替換

[oracle@rhel scripts]$ sed -n '5s/ORA/ora/2p' test1231.log 
ORA-01559ora-01560

                g-----對所有匹配到的內容進行替換

[oracle@rhel scripts]$ sed -n '5s/ORA/ora/g'p test1231.log
ora-01559ora-01560

                p-----會打印與替換命令匹配的行,通常與-n 一起使用

[oracle@rhel scripts]$ sed -n '5s/ORA/ora/2p' test1231.log 
ORA-01559ora-01560

                w file-----將緩衝區中的內容寫到指定的 file 文件中

[oracle@rhel scripts]$ sed  '5s/ORA/ora/w a.txt' test1231.log  
ORA-01555
ORA-01556
ORA-01557
ORA-01558fortest
ora-01559ORA-01560
[oracle@rhel scripts]$ more a.txt 
ora-01559ORA-01560

                \n-----匹配第 n 個子串,該子串之前在 pattern 中用 \(\) 指定
                \ -----轉義(轉義替換部分包含:&、\ 、/等),也可以用;來定界

[oracle@rhel scripts]$ sed -n '6s/\/etc\/passwd/\/etc\/passwdwd/'p test1231.log 
/etc/passwdwd
[oracle@rhel scripts]$ sed -n '6s;/etc/passwd;/etc/passwdwd;'p test1231.log   
/etc/passwdwd

2. sed '[address]d'
        d命令只是對輸出結果有效,對文件本身不做修改
        其中:
            address用來指定操作區間,用法同上

[oracle@rhel scripts]$ sed '2,3d' test1231.log  
ORA-01555
ORA-01558fortest
ORA-01559ORA-01560
/etc/passwd

3. sed '[address]a\str'   sed '[address]i\str'
        i是在指定行之前插入,a是在指定行之後插入

[oracle@rhel scripts]$ sed  -e '1,3a\ora-00001' -e '5,6i\testora-00002' test1231.log  
ORA-01555
ora-00001
ORA-01556
ora-00001
ORA-01557
ora-00001
ORA-01558fortest
testora-00002
ORA-01559ORA-01560
testora-00002
/etc/passwd

4. sed '[address]c\str' 
        c命令是將匹配到的行替換爲str

[oracle@rhel scripts]$ sed  -e '1,3c\ora-00001' -e '5,6c\testora-00002' test1231.log   
ora-00001
ORA-01558fortest
testora-00002

5. sed q
        q命令的作用是使 sed 命令在第一次匹配任務結束後,退出 sed 程序,不再進行對後續數據的處理。

[oracle@rhel scripts]$ sed '2q' test1231.log   
ORA-01555
ORA-01556

6. sed [address]y/str1/str2/
        y命令是對字符進行一對一映射轉換的,注意str1和str2的長度要保持一致,否則映射不上會報錯

[oracle@rhel scripts]$ sed  '2,4y/ORA/ARO/' test1231.log 
ORA-01555
ARO-01556
ARO-01557
ARO-01558fortest
ORA-01559ORA-01560
/etc/passwd

7. sed [address]r filename
        r命令是將一個文件內容整體插入到指定的行後

[oracle@rhel scripts]$ sed  '2,4r test12312.log' test1231.log  
ORA-01555
ORA-01556
test1
test2
ORA-01557
test1
test2
ORA-01558fortest
test1
test2
ORA-01559ORA-01560
/etc/passwd

        使用$可以插入到數據流的末尾

[oracle@rhel scripts]$ sed  '$r test12312.log' test1231.log    
ORA-01555
ORA-01556
ORA-01557
ORA-01558fortest
ORA-01559ORA-01560
/etc/passwd
test1
test2

另外推薦篇寫的比較紮實的博客:https://linux.cn/article-10232-1.html

發佈了91 篇原創文章 · 獲贊 14 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章