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 在其经典著作 '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屋!