美文网首页程序员利器
SED 命令笔记一命令参数

SED 命令笔记一命令参数

作者: jimson_ma | 来源:发表于2020-02-23 20:27 被阅读0次

Sed 是一个流式编辑器(流编辑器),与其他编辑器不同的是他可以在输入流(文件或者输入管道)中对文件进行处理或者转换(增删改查)。

基本使用方法: sed SCRIPT INPUTFILE
以文本替换为例(也是最常见的用法): Script 以s开头表示文本替换:

echo 'hello world' >  input.txt
cat input.txt #hello world
sed 's/hello/word/' input.txt > output.txt
cat output.txt.  #world world

如果不指定input 文件则会从stdin 中获取内容, 下面三个命令的结果是相同的:

sed 's/hello/world/' input.txt > output.txt
sed 's/hello/world/' < input.txt > output.txt
cat input.txt | sed 's/hello/world/' > output.txt 
echo 'hello world' | sed 's/hello/world/' > output.txt 

常见参数:

-i : in-place模式,表示编辑文件而不是将其打印出来。 此命令在mac 和linux 有点不同,具体参见man sed

# for linux
sed -i 's/hello/world/' input.txt
cat input.txt #world world

#for mac os
sed -i '.bak' 's/hello/world/' input.txt
cat input.txt #world world
cat input.txt.bak #hello world

-e: 指定命令的SCRIPT内容。可以使用多次指定多个SCRIPT. 其实在SCRIPT中使用;也可以组合多个命令。 下面命令的结果是相同的。

echo -e 'hello world' | sed -e 's/hello/A/' -e 's/world/B/'  # A B
echo -e 'hello world' | sed 's/hello/A/' | sed 's/world/B/'  # A B
echo -e 'hello world' | sed 's/hello/A/;s/world/B/'  # A B

验证下命令是顺序执行的, 修改命令如下:

echo -e 'hello world' | sed -e 's/hello/A/' -e 's/A/B/'  # B world
echo -e 'hello world' | sed 's/hello/A/' | sed 's/A/B/'  # B world
echo -e 'hello world' | sed 's/hello/A/;s/A/B/'  # B world

-n:阻止命令输出到stdout

 echo -e 'hello world' | sed 's/hello/A/' # A world
 echo -e 'hello world' | sed -n 's/hello/A/' # ~~ nothing

-f:以文件的形式指定SCRIPT

echo 's/hello/world/' > script
echo -e 'hello world' | sed -f script # A world

指定多个目标文件

echo 'ahello' > output.txt
sed -e 's/hello/world/' input.txt output.txt
#world world
#aworld

相关文章

  • SED 命令笔记一命令参数

    Sed 是一个流式编辑器(流编辑器),与其他编辑器不同的是他可以在输入流(文件或者输入管道)中对文件进行处理或者转...

  • Sed(二)

    2018-12-05笔记 Sed附加命令 插入命令 语法为,sed '[address] action the_l...

  • sed命令

    sed命令 # sed [参数] ‘<条件>’ <文件名> [参数] -n 不再显示模式空间里的内容(默认是显示...

  • 2019-07-24 课堂笔记sed命令

    昨天回顾 使用grep命令取IP地址 今日总结 sed执行原理sed执行原理.jpg sed [参数] '条件指令...

  • 【linxu编程】Linux文本处理三剑客——sed 2

    sed d命令删除行 s命令查找替换 y命令字符转换 p命令打印 -n参数表示不打印没处理的行 a/i命令插入文本...

  • linux中sed的复习

    sed命令格式:sed [参数] '动作' 文件名 参数:-n 打印具体的某一行时需要配合动作' p '使用-i ...

  • linux || sed(2)

    调用sed有三种方式: 在命令行键入命令; 将sed命令插入脚本文件,然后调用sed; 将sed命令插入脚本文件,...

  • sed命令

    sed命令 对比用paste和tr命令将fastq文件转换为fasta文件 paste sed命令 sed用法

  • Linux 去除文件中空行的几种方式

    tr 命令 sed 命令 awk 命令 grep 命令

  • Linux运维常用

    网络命令 Vim命令 sed命令 grep 命令

网友评论

    本文标题:SED 命令笔记一命令参数

    本文链接:https://www.haomeiwen.com/subject/fgisqhtx.html