AIX上的Sed无法识别-i标志 [英] Sed on AIX does not recognize -i flag
问题描述
sed -i
是否可以在AIX上使用?
Does sed -i
work on AIX?
如果没有,如何在AIX上就地编辑文件?
If not, how can I edit a file "in place" on AIX?
推荐答案
-i
选项是 sed
命令。它不是 sed
的经典界面的一部分。
The -i
option is a GNU (non-standard) extension to the sed
command. It was not part of the classic interface to sed
.
您不能直接在AIX上进行原位编辑。您必须执行以下操作:
You can't edit in situ directly on AIX. You have to do the equivalent of:
sed 's/this/that/' infile > tmp.$$
mv tmp.$$ infile
您只能在以下位置处理一个文件像这样的时间,而 -i
选项允许您获得其参数列表中许多文件的结果。 -i
选项仅打包此事件序列。
You can only process one file at a time like this, whereas the -i
option permits you to achieve the result for each of many files in its argument list. The -i
option simply packages this sequence of events. It is undoubtedly useful, but it is not standard.
如果您编写了此脚本,则需要考虑如果命令被中断会发生什么情况;这无疑是有用的。特别是,您不想留下临时文件。这样会导致类似的情况:
If you script this, you need to consider what happens if the command is interrupted; in particular, you do not want to leave temporary files around. This leads to something like:
tmp=tmp.$$ # Or an alternative mechanism for generating a temporary file name
for file in "$@"
do
trap "rm -f $tmp; exit 1" 0 1 2 3 13 15
sed 's/this/that/' $file > $tmp
trap "" 0 1 2 3 13 15
mv $tmp $file
done
如果在运行 sed
时发生信号(HUP,INT,QUIT,PIPE或TERM),则将删除临时文件。完成 sed
后,在 mv
发生时,它将忽略信号。
This removes the temporary file if a signal (HUP, INT, QUIT, PIPE or TERM) occurs while sed
is running. Once the sed
is complete, it ignores the signals while the mv
occurs.
您仍然可以通过执行以下操作来增强此功能:在与源文件相同的目录中创建临时文件,而不是潜在地在完全不同的文件系统中创建该文件。
You can still enhance this by doing things such as creating the temporary file in the same directory as the source file, instead of potentially making the file in a wholly different file system.
另一个增强功能是允许在命令行上指定命令(在示例中为 sed's / this / that
)。
The other enhancement is to allow the command (sed 's/this/that'
in the example) to be specified on the command line. That gets trickier!
您可以查询Kernighan和Pike在经典文章中描述的覆盖
(shell)命令本书 UNIX编程环境 。
You could look up the overwrite
(shell) command that Kernighan and Pike describe in their classic book 'The UNIX Programming Environment'.
这篇关于AIX上的Sed无法识别-i标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!