如何在不创建sed中间文件的情况下替换?

时间:2021-02-03 15:28:22

I was doing some hands-on with the Unix sed command. I was trying out the substitution and append command, in a file. But the difficulty is, I have to create an intermediate file, and then do mv to rename it to the original file.

我正在使用Unix sed命令进行一些动手操作。我在文件中尝试替换和追加命令。但困难在于,我必须创建一个中间文件,然后执行mv将其重命名为原始文件。

Is there any way to do it at one shot in the same file?

有没有办法在同一个文件中一次性完成?

[root@dhcppc0 practice]# sed '1i\
 > Today is Sunday
 > ' file1 > file1

[root@dhcppc0 practice]# cat file1
[root@dhcppc0 practice]#

The file is deleted!

该文件已被删除!

[root@dhcppc0 practice]# sed 's/director/painter/' file1 > file1
[root@dhcppc0 practice]# cat file1

The file is deleted!

该文件已被删除!

3 个解决方案

#1


13  

GNU sed knows an option -i which does in-place edit of the given files.

GNU sed知道一个选项-i,它可以对给定文件进行就地编辑。

When doing an operation file1 > file1 what actually happens is, that the file is opened and truncated by the shell before the program (which gets it's name as argument) comes around reading anything from it.

在执行操作file1> file1时,实际发生的是,在程序(将其名称作为参数)之前,shell打开并截断该文件,从中读取任何内容。

Update:

更新:

sed's man page states the following on the -i option (thanks Delan for mentioning it):

sed的手册页在-i选项上声明了以下内容(感谢Delan提及它):

   -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if extension supplied)

#2


12  

Try this -

尝试这个 -

sed -i '' 's/originaltext/replacementtext/g' filename | cat filename

-i '' is meant for providing a backup file. If you are confident your replacement won't cause an issue you can put '' to pass no backup file

-i''用于提供备份文件。如果您确信您的更换不会导致问题,您可以将''传递给没有备份文件

/g is for replacing globally. If you have more than one originaltext in one line then with /g option will replace all else it will only replace the first.

/ g用于全局替换。如果你在一行中有多个原始文本,那么使用/ g选项将替换所有其他只会替换第一个原始文本。

#3


1  

sed -i.bak 's/director/painter/' file1

sed -i.bak的/导演/画家/'file1

 -i[SUFFIX], --in-place[=SUFFIX]

    edit files in place (makes backup if extension supplied)

#1


13  

GNU sed knows an option -i which does in-place edit of the given files.

GNU sed知道一个选项-i,它可以对给定文件进行就地编辑。

When doing an operation file1 > file1 what actually happens is, that the file is opened and truncated by the shell before the program (which gets it's name as argument) comes around reading anything from it.

在执行操作file1> file1时,实际发生的是,在程序(将其名称作为参数)之前,shell打开并截断该文件,从中读取任何内容。

Update:

更新:

sed's man page states the following on the -i option (thanks Delan for mentioning it):

sed的手册页在-i选项上声明了以下内容(感谢Delan提及它):

   -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if extension supplied)

#2


12  

Try this -

尝试这个 -

sed -i '' 's/originaltext/replacementtext/g' filename | cat filename

-i '' is meant for providing a backup file. If you are confident your replacement won't cause an issue you can put '' to pass no backup file

-i''用于提供备份文件。如果您确信您的更换不会导致问题,您可以将''传递给没有备份文件

/g is for replacing globally. If you have more than one originaltext in one line then with /g option will replace all else it will only replace the first.

/ g用于全局替换。如果你在一行中有多个原始文本,那么使用/ g选项将替换所有其他只会替换第一个原始文本。

#3


1  

sed -i.bak 's/director/painter/' file1

sed -i.bak的/导演/画家/'file1

 -i[SUFFIX], --in-place[=SUFFIX]

    edit files in place (makes backup if extension supplied)