Linux bash 中的“sed”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3984824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:45:12  来源:igfitidea点击:

"sed" command in bash

linuxbashshellubuntused

提问by ajsie

Could someone explain this command for me:

有人可以为我解释这个命令:

cat | sed -e 's,%,$,g' | sudo tee /etc/init.d/dropbox << EOF
   echo "Hello World"
EOF

What does the "sed" command do?

“sed”命令有什么作用?

采纳答案by Hyman Kelly

sedis the Stream EDitor. It can do a whole pile of really cool things, but the most common is text replacement.

sed流编辑器。它可以做一大堆非常酷的事情,但最常见的是文本替换。

The s,%,$,gpart of the command line is the sedcommand to execute. The sstands for substitute, the ,characters are delimiters (other characters can be used; /, :and @are popular). The %is the pattern to match (here a literal percent sign) and the $is the second pattern to match (here a literal dollar sign). The gat the end means to globally replace on each line (otherwise it would only update the first match).

s,%,$,g命令行的部分是sed要执行的命令。的s代表的替代品,所述,字符是定界符(其他字符均可使用; /:@是流行)。该%是要匹配的模式(这里是文字百分号),并且$是第二个模式匹配(此处文字美元符号)。所述g在末端手段globally取代在每行上(否则将只更新第一匹配)。

回答by Lekensteyn

It reads Hello World(cat), replaces all (g) occurrences of %by $and (over)writes it to /etc/init.d/dropboxas root.

它读取Hello World( cat),替换所有 ( g) 出现的%by$并(覆盖)以/etc/init.d/dropboxroot 身份将其写入。

回答by codaddict

Here sedis replacing all occurrences of %with $in its standard input.

sed是在其标准输入中替换所有出现的%with $

As an example

举个例子

$ echo 'foo%bar%' | sed -e 's,%,$,g'

will produce "foo$bar$".

将产生“foo$bar$”。

回答by Anil Vishnoi

sed is a stream editor. I would say try man sed.If you didn't find this man page in your system refer this URL:

sed 是一个流编辑器。我想说试试 man sed。如果你在你的系统中没有找到这个手册页,请参考这个 URL:

http://unixhelp.ed.ac.uk/CGI/man-cgi?sed

http://unixhelp.ed.ac.uk/CGI/man-cgi?sed