sed -i选项在solaris上不能工作

[英]sed -i option is not working on solaris


I am using sed to replace a line with NULL in a file. The command i used is

我正在使用sed将文件中的一行替换为NULL。我使用的命令是。

sed -i "s/.*shayam.*//g" FILE

This is working fine in linux. shayam is replaced with blank in the FILE. But when i used this in solaris it is showing some error.

这在linux中运行良好。文件中的shayam被替换为空格。但是当我在solaris中使用它时,它会显示一些错误。

sed: illegal option -- i

sed:非法选项i

How to use -i functionality of sed in solaris. Kindly help.

如何在solaris中使用-i功能的sed。请帮助。

4 个解决方案

#1


11  

The -i option is GNU-specific. The Solaris version does not support the option.

i选项是特定于gnui的。Solaris版本不支持这个选项。

You will need to install the GNU version, or rename the new file over the old one:

您将需要安装GNU版本,或者将新文件重命名为旧文件:

sed 's/.shayam.//g' FILE > FILE.new && mv FILE.new FILE

#2


11  

I just answered a similar question sed -i + what the same option in SOLARIS, but for those who find this thread instead (I saw it in the related thread section):

我刚刚回答了一个类似的问题sed -i +在SOLARIS中有什么相同的选项,但是对于那些找到这个线程的人来说(我在相关的线程部分看到了):

The main problem I see with most answers given is that it doesn't work if you want to modify multiple files. The answer I gave in the other thread:

我看到大多数给出的答案的主要问题是,如果您想修改多个文件,它不能工作。我在另一条线上给出的答案是:

It isn't exactly the same as sed -i, but i had a similar issue. You can do this using perl:

它与sed -i并不完全相同,但是我有一个类似的问题。您可以使用perl来实现这一点:

perl -pi -e 's/find/replace/g' file

doing the copy/move only works for single files. if you want to replace some text across every file in a directory and sub-directories, you need something which does it in place. you can do this with perl and find:

复制/移动只适用于单个文件。如果您想要在目录和子目录中的每个文件中替换一些文本,您需要一些适当的东西。您可以使用perl进行此操作,并找到:

find . -exec perl -pi -e 's/find/replace/g' '{}' \;

#3


3  

sed doesn't haven an -i option.

sed没有-i选项。

You are probably using some vendor-specific variant of sed. If you want to use the vendor-specific non-standardized extensions of your vendor-specific non-standardized variant of sed, you need to make sure that you install said vendor-specific non-standardized variant and need to make sure that you call it and don't call the standards-compliant version of sed that is part of your operating environment.

您可能正在使用一些特定于供应商的sed变体。如果您想要使用的特定于供应商的非标准扩展特定于供应商的非标准化的变体sed,您需要确保您安装表示特定于供应商的内部变量,需要确保你叫它,别叫sed的符合标准的版本是你的操作环境的一部分。

Note that as always when using non-standardized vendor-specific extensions, there is absolutely no guarantee that your code will be portable, which is exactly the problem you are seeing.

请注意,在使用非标准化的特定于供应商的扩展时,绝对不能保证您的代码是可移植的,这正是您所看到的问题。

In this particular case, however, there is a much better solution: use the right tool for the job. sed is a stream editor (that's why it is called "sed"), i.e. it is for editing streams, not files. If you want to edit files, use a file editor, such as ed:

然而,在这种情况下,有一个更好的解决方案:使用合适的工具完成任务。sed是一个流编辑器(这就是为什么它被称为“sed”),也就是说,它用于编辑流,而不是文件。如果您想编辑文件,请使用文件编辑器,如ed:

ed FILE <<-HERE
  ,s/.shayam.//g
  w
  q
HERE

See also:

参见:

#4


0  

Either cat the file or try <? Then pipe (|) the result to a temp file and if all goes well (&&) mv the tempfile to the original file.

查看文件或尝试

Example:

例子:

cat my_file | sed '!A!B!' > my_temp_file && mv my_temp_file my_file
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/11/08/d8970b1b0c5f9312f3919e710f6fb630.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告