sed -i选项有什么作用? [英] What does sed -i option do?
问题描述
我正在调试shell脚本,并试图找出由以下命令执行的任务:
I'm debugging a shell script and trying to find out the task performed by the following command:
sed -i '1,+999d' /home/org_user/data.txt
我需要将此命令更改为失败并显示以下错误:
I need to change this command as its failing with the following error:
illegal option sed -i
但是在进行更改之前,我需要了解BAU的功能。
But before changing, I need to understand the BAU functioning.
在这方面感谢任何投入。 / p>
Appreciate any inputs in this regard.
推荐答案
对此的适用用法如下。假设您有以下文件 file.txt
:
An applicable use of this is as follows. Say you have the following file file.txt
:
1, 2, 6, 7, "p"
我们想将 p替换为0。
We want to replace "p" with 0.
sed 's/"p"/0/g' file.txt
使用上面的命令只是将输出打印到命令行中。
Using the above simply prints the output into command line.
您会想为什么不重定向它呢?像这样将文本返回文件:
You'd think why not just redirect that text back into the file like this:
sed 's/"p"/0/g' file.txt > file.txt
不幸的是,由于重定向的性质,上面只会产生一个空白文件。
Unfortunately because of the nature of redirects the above will simply produce a blank file.
相反,必须为输出创建一个临时文件,稍后将覆盖原始文件,如下所示:
Instead a temp file must be created for the output which later overwrites the original file something like this:
sed 's/"p"/0/g' file.txt > tmp.txt && mv tmp.txt file.txt
而不是在sed上方执行冗长的解决方法-使用-我允许使用更简单的命令:
Instead of doing the long workaround above sed edit in place option with -i allows for a much simpler command:
sed -i 's/"p"/0/g' file.txt
这篇关于sed -i选项有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!