• Shell脚本文本三剑客之sed编辑器


    目录

    一、sed编辑器简介

    二、sed工作流程

    三、sed命令

    四、sed命令的使用

    1.sed打印文件内容(p)

    (1)打印文件所有行

    (2)打印文件指定行

    2.sed增加、插入、替换行(a、i、c)

    (1)a操作在指定行下增加行内容

    (2)i操作在指定行上插入行内容 

    (3)c操作指定行替换为一行内容

    4.sed删除文件内容(d)

    5.sed替换文件内容(s)

    6.sed使用命令文件执行(-f)

    7.sed直接修改文件内容(-i)

    8.sed扩充操作

    (1)替换处&代表需要替换内容处中正则表达式的内容

    (2)替换处\1\...\n\ 对应需要替换内容处每个括号中子表达式(可以是字符串和正则)

    (3)大小写转换

    (4)将多个文件内容放在一个文件中 

    (5)复制、剪切粘贴行内容


    一、sed编辑器简介

            sed是一种流编辑器,流编辑器会在编辑器处理数据前基于预先提供的一组规则来编辑数据。

            sed编辑器可以根据命令来处理数据流中的数据,通过多种转换修改流经它的文本,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。

            sed是一个面向字符流的非交互式编辑器,也就是说 sed 不允许用户与它进行交互操作。

    二、sed工作流程

    1.读取

            sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的内存缓冲区中(又称模式空间,pattern space)。

    2.执行

            默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。

    3.显示

            发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

    注:默认情况下所有的sed命令都是在模式空间内执行的,不会影响到文件中的内容,除非是用重定向输出到文件或使用-i选项。

    三、sed命令

    sed [选项] 操作 文件名...

    常用选项 

    选项作用
    -e表示用指定命令来处理输入的文本文件,一般在执行多个操作命令使用(只有一个操作命令时可省略)。
    -f表示用指定的脚本文件来处理输入的文本文件。
    -n禁止sed编辑自动输出,但可以与p命令一起使用完成输出。
    -i直接修改目标文本文件

    常用操作

    操作作用
    a插入,在当前行下面增加一行指定内容。
    i插入,在选定行上面插入一行指定内容。
    d删除,删除指定的行
    s替换,替换选定的字符
    c替换,将选定行替换为指定内容
    p打印,如果同时指定行,表示打印指定行,如果不指定行,则表示打印所有内容,如果有非打印字符,则以ASCII码输出。其通常与"-n"选项一起使用
    =打印行号
    l打印数据流中的文本和不可打印的ASCII码字符(比如结束符$、制表符\t)。

    执行多个操作的方法

    • sed -e '操作1' -e '操作2' ... 文件名
    • sed -e '操作1;操作2;...' 文件名

    四、sed命令的使用

    1.sed打印文件内容(p)

    (1)打印文件所有行

            sed本身有执行操作后自动打印的效果,-n禁用,p操作也可以打印,并且可以限定条件

    sed ' ' 文件名       

    sed -n 'p' 文件名       

    1. //使用sed自动打印
    2. [root@localhost1 sedtest]#sed '' file1
    3. one
    4. two
    5. three
    6. four
    7. five
    8. six
    9. seven
    10. eight
    11. nine
    12. ten
    13. //使用sed的p操作打印
    14. [root@localhost1 sedtest]#sed -n 'p' file1
    15. one
    16. two
    17. three
    18. four
    19. five
    20. six
    21. seven
    22. eight
    23. nine
    24. ten

    (2)打印文件指定行

    sed -n '指定条件p' 文件名

    sed -n '/字符串或正则表达式/p' 文件名

    1. //1.打印指定行(根据行号)
    2. [root@localhost1 sedtest]#sed -n '2p' file1 //打印第2行
    3. two
    4. //2.打印指定的多行(根据行号)
    5. [root@localhost1 sedtest]#sed -n '7,$p' file1 //打印从第7行到末尾行
    6. seven
    7. eight
    8. nine
    9. ten
    10. //3.打印多行后退出
    11. [root@localhost1 sedtest]#sed '3q' file1 //从头打印3行后退出
    12. one
    13. two
    14. three
    15. //4.打印指定行(根据字符串)
    16. [root@localhost1 sedtest]#sed -n '/ve/p' file1 //根据字符串s
    17. five
    18. seven
    19. //5.从指定行打印到包含指定字符串的行(根据字符串)
    20. [root@localhost1 sedtest]#sed -n '7,/nobody/p' /etc/passwd //打印7到包含nobody的行
    21. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    22. halt:x:7:0:halt:/sbin:/sbin/halt
    23. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    24. operator:x:11:0:operator:/root:/sbin/nologin
    25. games:x:12:100:games:/usr/games:/sbin/nologin
    26. ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    27. nobody:x:99:99:Nobody:/:/sbin/nologin
    28. //6.打印奇偶行
    29. [root@localhost1 sedtest]#sed -n 'n;p' file1 //n表示跳到下一行,先n实现输出偶数行
    30. two
    31. four
    32. six
    33. eight
    34. ten
    35. [root@localhost1 sedtest]#sed -n 'p;n' file1 //后n实现奇数行
    36. one
    37. three
    38. five
    39. seven
    40. nine
    41. //7.正则表达式表示
    42. //根据root开头或包含mail的输出行(包含扩展正则时sed一定需要加-r选项)
    43. [root@localhost1 sedtest]#sed -nr '/^root|mail/p' /etc/passwd
    44. root:x:0:0:root:/root:/bin/bash
    45. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    46. //从第8行打印到包含(r 至少有一个a t)的字符串的行
    47. [root@localhost1 sedtest]#sed -nr '8,/ra{1,}t/p' /etc/passwd
    48. halt:x:7:0:halt:/sbin:/sbin/halt
    49. mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    50. operator:x:11:0:operator:/root:/sbin/nologin

    2.sed增加、插入、替换行(a、i、c)

    (1)a操作在指定行下增加行内容

    sed '指定行范围a 指定内容' 文件名

    1. //指定第2行下增加行内容
    2. [root@localhost1 ~]#sed '2a ABC' file1
    3. One Two Three
    4. Four Five Six
    5. ABC
    6. Seven Eight Nine
    7. Ten Eleven Twelve
    8. //指定2到4行每行下增加行内容
    9. [root@localhost1 ~]#sed '2,4a ABC' file1
    10. One Two Three
    11. Four Five Six
    12. ABC
    13. Seven Eight Nine
    14. ABC
    15. Ten Eleven Twelve
    16. ABC

    (2)i操作在指定行上插入行内容 

    sed '指定行范围i 指定内容' 文件名

    1. //指定第2行上插入行内容
    2. [root@localhost1 ~]#sed '2i ABC' file1
    3. One Two Three
    4. ABC
    5. Four Five Six
    6. Seven Eight Nine
    7. Ten Eleven Twelve
    8. //指定第1到3行上插入行内容
    9. [root@localhost1 ~]#sed '1,3i ABC' file1
    10. ABC
    11. One Two Three
    12. ABC
    13. Four Five Six
    14. ABC
    15. Seven Eight Nine
    16. Ten Eleven Twelve

    (3)c操作指定行替换为一行内容

     sed '指定行范围c 指定内容' 文件名

    1. //指定第3行替换为指定行内容
    2. [root@localhost1 ~]#sed '3c ABC' file1
    3. One Two Three
    4. Four Five Six
    5. ABC
    6. Ten Eleven Twelve
    7. //指定第3到4行替换为指定行内容(多行变为一行)
    8. [root@localhost1 ~]#sed '3,4c ABC' file1
    9. One Two Three
    10. Four Five Six
    11. ABC
    12. //指定包含ve的行替换为指定行内容
    13. [root@localhost1 ~]#sed '/ve/c ABC' file1
    14. One Two Three
    15. ABC
    16. ABC
    17. ABC

    4.sed删除文件内容(d)

    sed '指定条件d' 文件名

    sed '/字符串或正则表达式/d' 文件名

    1. //1.删除所有行(根据行号)
    2. [root@localhost1 sedtest]#sed 'd' file1
    3. //2.删除指定行(根据行号)
    4. [root@localhost1 sedtest]#sed '1d' file1 //删除第1行
    5. two
    6. three
    7. four
    8. five
    9. six
    10. seven
    11. eight
    12. nine
    13. ten
    14. //3.删除指定的多行(根据行号)
    15. [root@localhost1 sedtest]#sed '2,8d' file1 //删除2到8行
    16. one
    17. nine
    18. ten
    19. //4.删除空行
    20. [root@localhost1 sedtest]#sed '/^$/d' /etc/fstab
    21. #
    22. # /etc/fstab
    23. # Created by anaconda on Fri Jul 29 21:42:19 2022
    24. #
    25. # Accessible filesystems, by reference, are maintained under '/dev/disk'
    26. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    27. #
    28. /dev/mapper/centos-root / xfs defaults 0 0
    29. UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
    30. /dev/mapper/centos-swap swap swap defaults 0 0
    31. //5.删除指定的多行(根据字符串)
    32. [root@localhost1 sedtest]#sed '/ve/d' file1 //删除包含ve的行
    33. one
    34. two
    35. three
    36. four
    37. six
    38. eight
    39. nine
    40. ten
    41. //6.正则表达式表示
    42. //删除结尾为bash或nologin的行
    43. [root@localhost1 sedtest]#sed -r '/bash$|nologin$/d' /etc/passwd
    44. sync:x:5:0:sync:/sbin:/bin/sync
    45. shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    46. halt:x:7:0:halt:/sbin:/sbin/halt
    47. //7.从包含第1个字符串的行开始删除,到包含第2个字符串的位置停止删除
    48. //(如果找不到第2个字符串的行,则删到最后一行)
    49. [root@localhost1 sedtest]#sed '/one/,/five/d' file1 //从含one的行删到含five的行
    50. six
    51. seven
    52. eight
    53. nine
    54. ten
    55. [root@localhost1 sedtest]#sed '/thr/,/no/d' file1 //从含thr的行开始删,匹配不到含no的行,则删到最后
    56. one
    57. two
    58. //8.反向删除(删除除了包含指定内容的行)
    59. [root@localhost1 sedtest]#sed '/ve/d' file1 //删除除包含ve的行
    60. five
    61. seven

    5.sed替换文件内容(s)

    sed [选项] '行范围 s/旧字符串/新字符串/替换标记' 文件名

    4种替换标记

    替换标记作用
    数字n表明新字符串将替换第n处匹配的地方
    g表明新字符串将会替换所有匹配的地方
    p打印与替换命令匹配的行,需要在前面加 -n选项
    w将替换的结果写到文件中
    1. //1.不加范围,替换每行查找到的字符串
    2. [root@localhost1 ~]#sed -n 's/root/admin/p' /etc/passwd
    3. admin:x:0:0:root:/root:/bin/bash
    4. operator:x:11:0:operator:/admin:/sbin/nologin
    5. //2.指定范围行,在指定行中替换n次(默认1次)
    6. [root@localhost1 ~]#sed -n '/^root/ s/root/abc/p' /etc/passwd
    7. abc:x:0:0:root:/root:/bin/bash
    8. [root@localhost1 ~]#sed -n '/^root/ s/root/abc/2p' /etc/passwd
    9. root:x:0:0:abc:/root:/bin/bash
    10. //3.替换行中所有,使用g
    11. [root@localhost1 ~]#sed -n '/^root/ s/root/abc/gp' /etc/passwd
    12. root:x:0:0:root:/abc:/bin/bash
    13. //4.指定行s/^/#/可以实现注释行内容
    14. [root@localhost1 ~]#sed '2 s/^/#/' file1
    15. one two three
    16. #four five six
    17. seven eight nine
    18. ten eleven twelve
    19. //5.指定行范围替换
    20. [root@localhost1 ~]#sed '1,3 s/^/#/' file1
    21. #one two three
    22. #four five six
    23. #seven eight nine
    24. ten eleven twelve
    25. //指定替换除为空可以实现将指定字符串删除
    26. [root@localhost1 ~]#sed 's/#//' /etc/fstab
    27. /etc/fstab
    28. Created by anaconda on Fri Jul 29 21:42:19 2022
    29. Accessible filesystems, by reference, are maintained under '/dev/disk'
    30. See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    31. /dev/mapper/centos-root / xfs defaults 0 0
    32. UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
    33. /dev/mapper/centos-swap swap swap defaults 0 0

    6.sed使用命令文件执行(-f)

    1. //编写文件包含sed的操作
    2. [root@localhost1 ~]#vim script.txt
    3. /^$/d
    4. /e$/ s/^/#/
    5. 1,3 s/#//
    6. //测试用文件
    7. [root@localhost1 ~]#vim file2
    8. #one
    9. #two
    10. #three
    11. four
    12. five
    13. six
    14. seven
    15. eight
    16. nine
    17. ten
    18. //sed指定文件操作
    19. [root@localhost1 ~]#sed -f script.txt file2
    20. #one
    21. two
    22. #three
    23. four
    24. five
    25. six
    26. seven
    27. eight
    28. #nine
    29. ten

    7.sed直接修改文件内容(-i)

    1. //加-i直接对文件进行操作
    2. [root@localhost1 ~]#sed -i '/swap/ s/^/#/' /etc/fstab
    3. //注释文件中包含swap的行
    4. [root@localhost1 ~]#vim /etc/fstab
    5. # /etc/fstab
    6. # Created by anaconda on Fri Jul 29 21:42:19 2022
    7. #
    8. # Accessible filesystems, by reference, are maintained under '/dev/disk'
    9. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    10. #
    11. /dev/mapper/centos-root / xfs defaults 0 0
    12. UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot xfs defaults 0 0
    13. #/dev/mapper/centos-swap swap swap defaults 0 0

    8.sed扩充操作

    (1)替换处&代表需要替换内容处中正则表达式的内容

    sed [选项] '行范围 s/正则表达式/&/替换标记' 文件名

    1. //这里的&代表.*swap.*匹配到的内容(即包含swap的行内容),在前面加上#可以实现注释行
    2. [root@localhost1 ~]#sed -n 's/.*swap.*/#&/' /etc/fstab
    3. #/dev/mapper/centos-swap swap swap defaults 0 0

    (2)替换处\1\...\n\ 对应需要替换内容处每个括号中子表达式(可以是字符串和正则)

    sed [选项] 's/(表达式1)..(表达式n)/\1\2\...\n/' 文件名

    1. //原文件
    2. [root@localhost1 ~]#cat file3
    3. 123456abcdef
    4. //字符串子表达示,"\数字" 一一对应
    5. [root@localhost1 ~]#sed 's/(123)(456)(abc)/\3\2\1/' file3
    6. abc456123def
    7. //正则子表达示,"\数字" 一一对应
    8. [root@localhost1 ~]#sed -r 's/([0-9]{6})([a-z]{6})/\2\1/' file3
    9. abcdef123456

    (3)大小写转换

    sed 's/[a-z]/\u&/g' 文件名  (原字符范围前\b实现首字母转大写)

    sed 's/[A-Z]/\l&/g' 文件名

    1. //原文件
    2. [root@localhost1 ~]#cat file1
    3. one two three
    4. four five six
    5. seven eight nine
    6. ten eleven twelve
    7. //1.全部转为大写
    8. [root@localhost1 ~]#sed 's/[a-z]/\u&/g' file1
    9. ONE TWO THREE
    10. FOUR FIVE SIX
    11. SEVEN EIGHT NINE
    12. TEN ELEVEN TWELVE
    13. [root@localhost1 ~]#sed -i 's/[a-z]/\u&/g' file1
    14. //2.全部转为小写
    15. [root@localhost1 ~]#sed 's/[A-Z]/\l&/g' file1
    16. one two three
    17. four five six
    18. seven eight nine
    19. ten eleven twelve
    20. //3.锚定首字母,实现首字母大小写转换
    21. [root@localhost1 ~]#sed 's/\b[a-z]/\u&/g' file1
    22. One Two Three
    23. Four Five Six
    24. Seven Eight Nine
    25. Ten Eleven Twelve

    /U/对应子表达式位置数/E   (结合第2条操作实现部分转大小写)

    1. //部分转大小写,实现修改文件前缀名小写转大写
    2. [root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\u\1\E\2/' //u转开头
    3. Abc.txt
    4. A.txt
    5. B.txt
    6. Efg.txt
    7. Number.txt
    8. Script.txt
    9. [root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\U\1\E\2/' //U转全部
    10. ABC.txt
    11. A.txt
    12. B.txt
    13. EFG.txt
    14. NUMBER.txt
    15. SCRIPT.txt

    (4)将多个文件内容放在一个文件中 

    sed -i '$r 文件1' 文件2

    1. //往fa、fb文件输入内容
    2. [root@localhost1 ~]#echo 12345678 > fa
    3. [root@localhost1 ~]#echo abcdefgh > fb
    4. //合并文件内容到fb文件中
    5. [root@localhost1 ~]#sed -i '$r fa' fb
    6. [root@localhost1 ~]#cat fb
    7. abcdefgh
    8. 12345678

    (5)复制、剪切粘贴行内容

    H 复制  ; G 粘贴

    1. //原文件
    2. [root@localhost1 ~]#cat file2
    3. zhang san
    4. li si
    5. wang wu
    6. zhao liu
    7. qian qi
    8. zhu ba
    9. //1.复制粘贴到末尾
    10. [root@localhost1 ~]#sed '1,2H;$G' file2
    11. zhang san
    12. li si
    13. wang wu
    14. zhao liu
    15. qian qi
    16. zhu ba
    17. zhang san
    18. li si
    19. //2.剪切粘贴到末尾
    20. [root@localhost1 ~]#sed '1,2{H;d};$G' file2
    21. wang wu
    22. zhao liu
    23. qian qi
    24. zhu ba
    25. zhang san
    26. li si

  • 相关阅读:
    模拟电子技术一|发展史
    零基础制作预约小程序,微信小程序预约服务指南
    openGauss数据库的安装部署
    520. 检测大写字母
    云原生架构实践前言
    Python 常用的内置模块
    harbor私有仓库部署
    卷积神经网络 -多输入多输出通道
    Convai:让虚拟游戏角色更智能的对话AI人工智能平台
    javaweb企业项目管理系统
  • 原文地址:https://blog.csdn.net/weixin_58544496/article/details/126698478