Linux中sed命令使用

1、sed介绍

sed(Stream Editor)是一种流文本编辑器,它可以在输入文件中执行基本文本转换。它可以将文本中的字符、行和单词进行删除、替换和添加等操作,而无需更改原始文件。

sed语法:

sed [选项] 'script' input_file

script:由一个或多个sed编辑命令组成的脚本。每个编辑命令可以包括地址或正则表达式,用于指定应用命令的行。

input_file:要处理的输入文件。

用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...

  -n, --quiet, --silent
                 取消自动打印模式空间
      --debug
                 对程序运行进行标注
  -e 脚本, --expression=脚本
                 添加“脚本”到程序的运行列表
  -f 脚本文件, --file=脚本文件
                 添加“脚本文件”到程序的运行列表
  --follow-symlinks
                 直接修改文件时跟随软链接
  -i[扩展名], --in-place[=扩展名]
                 直接修改文件(如果指定扩展名则备份文件)
  -l N, --line-length=N
                 指定“l”命令的换行期望长度
  --posix
                 关闭所有 GNU 扩展
  -E, -r, --regexp-extended
                 在脚本中使用扩展正则表达式
                 (为保证可移植性使用 POSIX -E)。
  -s, --separate
                 将输入文件视为各个独立的文件而不是单个
                 长的连续输入流。
      --sandbox
                 在沙盒模式中进行操作(禁用 e/r/w 命令)。
  -u, --unbuffered
                 从输入文件读取最少的数据,更频繁的刷新输出
  -z, --null-data
                 使用 NUL 字符分隔各行
      --help     打印帮助并退出
      --version  输出版本信息并退出

  • 参数选项:

    • -e:允许在同一行上指定多个编辑命令。
    • -n:取消自动打印模式空间的内容。
    • -i:直接修改文件内容,而不是在标准输出中显示结果(谨慎使用)。

2、常用的sed使用示例

(1)替换文本

    这将在1.txt中将所有的“is”替换为“iis”。

sed 's/is/iis/g' 1.txt

ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
this is test
yes or no
create a xptxt  is

ubuntu@VM-12-3-ubuntu:~$ sed 's/is/iis/g' 1.txt
thiis iis test
yes or no
create a xptxt  iis
ubuntu@VM-12-3-ubuntu:~$

如下,加了-i参数是直接永久修改原始文件,不加-i是直接显示修改效果,确定更改之前最好先备份!

ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
this is test
yes or no
create a xptxt  is
ubuntu@VM-12-3-ubuntu:~$ sed -i 's/is/iis/g' 1.txt
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis
ubuntu@VM-12-3-ubuntu:~$

提示:在macOS系统中,你需要指定一个空字符串作为备份文件的后缀名,否则会报错。例如

sed -i '' 's/old/new/g' file.txt

(2)删除匹配行

sed '/yes/d' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis

# 删除包含“yes”的所有行
ubuntu@VM-12-3-ubuntu:~$ sed '/yes/d' 1.txt
thiis iis test
create a xptxt  iis
ubuntu@VM-12-3-ubuntu:~$

(3)插入文本到特定行之前

sed '2i\ new line' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis

# 将在第3行之前插入“new line”
ubuntu@VM-12-3-ubuntu:~$ sed '2i\new line' 1.txt
thiis iis test
new line
yes or no
create a xptxt  iis
ubuntu@VM-12-3-ubuntu:~$

(4)打印特定行

sed -n '3p' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis

# 只打印第3行
ubuntu@VM-12-3-ubuntu:~$ sed -n '3p' 1.txt
create a xptxt  iis
ubuntu@VM-12-3-ubuntu:~$

(5)多个命令组合

sed -e 's/yes/nono/g' -e '2,4d' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis
qwe
rtp and res

# 将先进行替换,然后删除第2到第4行的内容
ubuntu@VM-12-3-ubuntu:~$ sed -e 's/yes/nono/g' -e '2,4d' 1.txt
thiis iis test
rtp and res
ubuntu@VM-12-3-ubuntu:~$

(6)在匹配行后面添加文本

sed '/res/a\new line' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis
qwe
rtp and res

# 在包含“res”的行后面添加“new line”
ubuntu@VM-12-3-ubuntu:~$ sed '/res/a\new line' 1.txt
thiis iis test
yes or no
create a xptxt  iis
qwe
rtp and res
new line
ubuntu@VM-12-3-ubuntu:~$

(7)替换指定行的内容

sed '3c\11111' 1.txt

# 原文本内容
ubuntu@VM-12-3-ubuntu:~$ cat 1.txt
thiis iis test
yes or no
create a xptxt  iis
qwe
rtp and res


将第3行的内容替换为“11111”:
ubuntu@VM-12-3-ubuntu:~$ sed '3c\11111' 1.txt
thiis iis test
yes or no
11111
qwe
rtp and res
ubuntu@VM-12-3-ubuntu:~$

(8)如何将 test1 test2 test3 改成"test1","test2","test3"

ubuntu@VM-12-3-ubuntu:~$ cat 2.txt
test1 test2 test3

ubuntu@VM-12-3-ubuntu:~$ sed -i 's/^/"/' 2.txt |sed -i 's/ /","/g' 2.txt |sed -i 's/$/"/' 2.txt
ubuntu@VM-12-3-ubuntu:~$ cat 2.txt
test1","test2","test3"

注意:以上都没有使用-i参数直接修改文件内容,而是将修改后的结果输出到标准输出,根据情况进行使用修改!!

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
sed命令是一种Linux/Unix操作系统下的文本处理工具,可以用来对文本文件进行增删改查等操作。 常用的sed命令包括: 1. 替换命令:s s命令用于替换文本的字符串,语法为: ``` sed 's/oldstring/newstring/g' filename ``` 其,oldstring表示要替换的字符串,newstring表示要替换成的字符串,g表示全局替换(即一行所有匹配的字符串都会被替换)。 2. 删除命令:d d命令用于删除文本的某些行,语法为: ``` sed 'num1,num2d' filename ``` 其,num1和num2表示要删除的行号范围。 3. 插入命令:i i命令用于在文本的某些行前插入新的内容,语法为: ``` sed 'num1inewline' filename ``` 其,num1表示要插入的行号,newline表示要插入的新内容。 4. 追加命令:a a命令用于在文本的某些行后追加新的内容,语法为: ``` sed 'num1anewline' filename ``` 其,num1表示要追加的行号,newline表示要追加的新内容。 5. 打印命令:p p命令用于打印文本的某些行,语法为: ``` sed -n 'num1,num2p' filename ``` 其,num1和num2表示要打印的行号范围,-n表示只打印指定行,不打印其他行。 6. 替换文件命令:-i -i命令用于直接修改文件内容,语法为: ``` sed -i 's/oldstring/newstring/g' filename ``` 其,oldstring表示要替换的字符串,newstring表示要替换成的字符串,g表示全局替换(即一行所有匹配的字符串都会被替换),-i表示直接修改文件内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值