Linux 使用sed在每行的开头插入文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4080526/
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:48:32  来源:igfitidea点击:

Using sed to insert text at the beginning of each line

linuxunixshellsedawk

提问by THE DOCTOR

How would one go about using sed in order to insert

如何使用 sed 来插入

rm -rf

at the start of each line of a file?

在文件每一行的开头?

采纳答案by mikerobi

sed 's/^/rm -rf /' filename

EDIT

编辑

Xargs would be simpler way to delete all of the files listed in another file

Xargs 是删除另一个文件中列出的所有文件的更简单的方法

xargs -a filename rm -rf

回答by Matt Sonnentag

Sometimes you have to go with the original method using sed, especially when you want to do things like

有时您必须使用 sed 使用原始方法,尤其是当您想要执行以下操作时

csf -d ipaddr.

xargsdoesn't seem to like the output created by some commands and gives up after the first line. ie:

xargs似乎不喜欢某些命令创建的输出并在第一行之后放弃。IE:

sed 's/^/csf -d /' hacks >>hacks.sh