章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • linux

    • rsync

    • dns

    • sed、awk、grep、find四剑客

      • sed命令在文本每行,行尾或行首添加字符
      • 日志分析
      • grep参数使用示例
      • sed常用参数和使用示例
        • awk常用参数和使用示例
      • LVM管理
      • sudo权限规划
      • linux修改网卡为eth0的两种方法
      • Logrotate入门了解及生产实践
      • linux中用dd命令来测试硬盘读写速度
      • linux 阿里云盘挂载错误
      • CentOS7安装Android SDK
      • centos7安装更新git
      • linux启动顺序
      • centos7升级openssl
      • expect工具的安装和使用方法
      • linux下使用v2ray
      • centos7安装java环境的两种方式
      • linux-centos7系统设置时区及同步时间
      • rsyslog日志系统:rsyslog配置文件
      • rsyslog的安装、使用、详解
      • safe-rm防止删除重要的文件
      • linux如何获取打开文件和文件描述符数量
      • LVS集群-DR模式
      • linux服务器安装ffmpeg
      • linux服务器安装samba
      • 使用openssl创建自签发证书
      • linux服务器部署next.js服务
      • linux服务器ionice命令使用方式
      • linux服务器curl命令常用操作
    • windows

    • 中间件

    • 网络

    • 安全

    • 存储

    • 防火墙

    • 数据库

    • 系统

    • docker

    • other

    • 监控

    • 运维
    • linux
    • sed、awk、grep、find四剑客
    章工运维
    2024-07-24
    目录

    sed常用参数和使用示例

    # 常用参数

    -e: 指定要执行的编辑命令。可以多次使用来指定多个命令。

    sed -e 's/foo/bar/' file

    -i: 直接在文件中修改内容(即“就地编辑”)。可以使用备份扩展名来保留原文件。

    sed -i.bak 's/foo/bar/' file # 修改文件,并保留原文件的备份

    -n: 禁用自动输出模式空间的内容。通常与 p(打印)命令配合使用。

    sed -n 's/foo/bar/p' file # 只输出替换了内容的行

    -f: 从文件中读取编辑命令,而不是在命令行中指定。

    sed -f script.sed file

    -r 或 --regexp-extended: 使用扩展的正则表达式(ERE),支持更复杂的模式匹配。

    sed -r 's/(foo|bar)/baz/' file

    -e: 指定要执行的编辑命令。如果你有多个命令,可以使用这个选项多次。

    sed -e 's/foo/bar/' -e 's/baz/qux/' file

    # 常用命令

    1. s/pattern/replacement/: 替换匹配 pattern 的内容为 replacement。默认只替换每行的第一个匹配项。

    sed 's/foo/bar/' file

    1. p: 打印模式空间中的内容。通常与 -n 一起使用以避免自动打印所有行。

    sed -n 'p' file # 打印所有行

    1. d: 删除模式空间中的内容。

    sed '/pattern/d' file # 删除匹配 pattern 的行

    1. a\ text: 在匹配的行之后添加文本。

    sed '/pattern/a\New text' file

    1. i\ text: 在匹配的行之前插入文本。

    sed '/pattern/i\New text' file

    1. c\ text: 用 text 替换匹配的行。

    sed '/pattern/c\New text' file

    1. g: 用于全局替换,通常在 s 命令中使用,替换每行中的所有匹配项。

    sed 's/foo/bar/g' file

    # 示例

    1. 将文件 file.txt 中的所有 "foo" 替换为 "bar" 并保存更改:

    sed -i 's/foo/bar/g' file.txt

    1. 仅打印包含 "foo" 的行:

    sed -n '/foo/p' file.txt

    1. 在每行以 "Hello" 结尾添加 "World":

    sed 's/$/ World/' file.txt

    1. 删除包含 "foo" 的行并保存更改:

    sed -i '/foo/d' file.txt

    这些是 sed 的一些基本用法。它非常强大,可以处理复杂的文本处理任务。

    微信 支付宝
    上次更新: 2024/07/24, 20:56:50

    ← grep参数使用示例 awk常用参数和使用示例→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式