AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 467361
Accepted
somethingSomething
somethingSomething
Asked: 2018-09-07 10:19:46 +0800 CST2018-09-07 10:19:46 +0800 CST 2018-09-07 10:19:46 +0800 CST

sed 有多个命令,我可以使用 w(写)标志吗?

  • 772

我在这里学习sed,这里有多个命令:

(user@host)-(18:27:39)-(~/Bash_Programming)
$sed '4 { s/fox/elephant/; s/dog/cat/ }' catAndDog.txt 
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown elephant jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

但是,如果我使用多个命令并希望将结果写入文件,我应该将 w 替换标志放在哪里?

w file = write the result of the substitution to a file

编辑:

我想将整个输出写入文件,即原始文件的副本以及所做的更改。

text-processing sed
  • 3 3 个回答
  • 1107 Views

3 个回答

  • Voted
  1. αғsнιη
    2018-09-07T11:01:06+08:002018-09-07T11:01:06+08:00

    回应:

    我想将整个输出写入文件,即原始文件的副本以及所做的更改。

    您可以重定向到像这样的文件

    sed '4{ s/fox/elephant/; s/dog/cat/}' infile > modified_file
    

    -i或使用标志就地修改:

    sed -i '4{ s/fox/elephant/; s/dog/cat/}' infile
    

    ...或从原始文件中备份并使用相同的原始文件名写入更改。

    sed -i.backup '4{ s/fox/elephant/; s/dog/cat/}' infile
    

    或使用w命令:

    sed '4{ s/fox/elephant/; s/dog/cat/ }; w modified_file' infile
    

    如果您只想编写 s命令替换成功的行(这不是您当前要求的),使用该w标志可以执行以下操作:

    sed '4{ s/fox/elephant/; s/dog/cat/w modified_file
    }' infile
    
    • 2
  2. user286944
    2018-09-10T12:43:56+08:002018-09-10T12:43:56+08:00

    但是,如果我使用多个命令并希望将结果写入文件,我应该将 w 替换标志放在哪里?

    该w标志必须以换行符终止,或者必须是 sed 脚本中的最后一项(包括它的参数)。如果找到空格、注释或分号,它们将包含在文件名中。

    不要混淆与w命令替换s( s///w) 和w命令相关的标志。它们相似,但w仅在执行替换时才使用该标志。请注意,w根据 sed 脚本(用例),可以有条件地或无条件地使用该命令。

    注意:选项i( --in-place) 不是由 POSIX 指定的。

    第一个例子

    prompt% sed -i.back -e "/$regex/w output" -e "/$regex/d" input
    

    第二个例子

    prompt% sed -i.back "/$regex/ {
    w output
    d;}" input
    

    有人可能会遇到与您的案例有关的问题(见下文),因为在同一输入行上执行了多个替换。

    prompt% sed -n '4 { 
    s/fox/wolf/w output
    s/dog/bear/w output
    }' input
    

    只修改了一个输入行,但输出文件包含两行。

    The quick brown wolf jumps over the lazy dog
    The quick brown wolf jumps over the lazy bear
    

    这个结果不方便,因为它具有误导性。

    我想将整个输出写入文件,即原始文件的副本以及所做的更改。

    我的解决方案允许创建原始文件的备份input.back(该算法使用命令。inputoutputw

    prompt% sed -i.back -nE '4!p       # print line except 4th
    4!d                                # delete line except 4th
    /fox|dog/!p                        # print line if no "fox" or no "dog"
    /fox|dog/!d                        # don't delete line if "fox" or "dog"
    s/fox/wolf/                        # substitution occurs if "fox"
    s/dog/bear/                        # substitution occurs if "dog"
    p                                  # if a substitution then print 4th line
    w output' input                    # if a change then save 4th line in "output"
    

    如果找到或未找到(均未找到),该命令将/fox|dog/!d立即开始下一个循环。因此,如果未找到关键字,则不会为当前行处理以下命令( 、、 )。foxdogs///pw

    • 2
  3. Best Answer
    user232326
    2018-09-10T23:43:06+08:002018-09-10T23:43:06+08:00

    实际上,有两个 w要素。一个是命令,另一个是s///旗帜。

    两者都用于将当前模式空间写入文件。

    命令

    例如,此w命令将写入outfile(制作副本):

    $ printf '%s\n' AA BB CC DD EE > infile
    $ sed -n 'w outfile' infile
    $ cat outfile
    AA
    BB
    CC
    DD
    EE
    

    该-n选项避免打印任何模式空间,但该w命令将write每个模式(行)到命令之后的参数中的文件名w(应该结束命令)。

    相反,这将只写一行(具有(至少) one 的行C):

    $ sed -n '/C/ w outfile' infile ;  cat outfile
    CC
    

    旗帜

    另一个w元素是命令的w标志s///,尽管它的含义大致相同write,但只有在s///命令中的替换被执行时才会执行:

    $ sed -n 's/C/X/w outfile' infile; cat outfile
    XC
    

    第一季度

    但是,如果我使用多个命令并希望将结果写入文件,我应该将 w 替换标志放在哪里?

    由于替换标志连接到一个(特定)替换,它应该在哪里使用取决于需要记录哪个替换:

    $ sed -n -e 's/B/X/w outfile' -e 's/D/Y/' infile; cat outfile
    XB
    

    请注意,每个替换必须在单独的-e(或单独的行)中,因为输出文件名的名称必须结束命令(需要换行符),或者可以写成:

    $ sed -n -e 's/B/X/w outfile
    > s/D/Y/' infile; cat outfile
    XB
    

    可以选择其他替换:

    $ sed -n -e 's/B/X/' -e 's/D/Y/w outfile' infile; cat outfile
    YD
    

    或两者:

    $ sed -n -e 's/B/X/w outfile' -e 's/D/Y/w outfile' infile; cat outfile
    XB
    YD
    

    或使用w命令(不是标志)(并假设 GNU sed):

    $ sed -n 's/B/X/;s/D/Y/;w outfile' infile; cat outfile
    AA
    XB
    CC
    YD
    EE
    

    第二季度

    我想将整个输出写入文件,即原始文件的副本以及所做的更改。

    除非所有行都发生了替换,否则替换标志不能写入整个文件。您需要使用w命令(假设 bash):

    $ printf 'The quick brown fox jumps over the lazy dog%.0s\n' {1..14} >catAndDog.txt
    $ sed -n '4 { s/fox/elephant/ ; s/dog/cat/ } ; w outfile' catAndDog.txt
    $ cat outfile
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown elephant jumps over the lazy cat
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    The quick brown fox jumps over the lazy dog
    

    了解每个替换的w标志将为每个替换打印一行,因此会发生这种情况:

    $ sed -n -e '4 { s/fox/elephant/w outfile' -e 's/dog/cat/w outfile' -e '}' catAndDog.txt
    $ cat outfile
    The quick brown elephant jumps over the lazy dog
    The quick brown elephant jumps over the lazy cat
    
    • 2

相关问题

  • 如何删除两行之间的单行

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve