11

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.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Raj
  • 757
  • 4
  • 11
  • 22

4 Answers4

13

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
camh
  • 40,988
  • 13
  • 62
  • 70
  • Using mktemp helps to ensure that we don't inadvertantly step on any files named FILE.new ... tmp=$(mktemp) && sed -e 's/.*shayam.*//g' FILE > "$tmp" && mv "$tmp" FILE; rm "$tmp" 2> /dev/null – Michael Back Nov 26 '15 at 04:47
12

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' '{}' \;
Community
  • 1
  • 1
Charlie B
  • 525
  • 4
  • 6
3

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:

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
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
user268396
  • 11,576
  • 2
  • 31
  • 26