Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed -i option is not working on solaris

Tags:

unix

sed

solaris

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

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.

sed: illegal option -- i

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

like image 614
Raj Avatar asked Nov 08 '10 06:11

Raj


3 Answers

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):

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:

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:

find . -exec perl -pi -e 's/find/replace/g' '{}' \;
like image 189
Charlie B Avatar answered Nov 02 '22 22:11

Charlie B


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

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

sed 's/.shayam.//g' FILE > FILE.new && mv FILE.new FILE
like image 39
camh Avatar answered Nov 02 '22 21:11

camh


sed doesn't haven an -i option.

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.

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:

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

See also:

  • Unable to use SED to edit files fast
  • How can I replace a specific line by line number in a text file?
like image 38
Jörg W Mittag Avatar answered Nov 02 '22 21:11

Jörg W Mittag