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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值