-1

I have files that contain a string OLD. I want to replace this string with NEW. I want to do that not just in current directory but in all sub directories, so recursively.

The thing is, I am on Solaris (v11.3) and the following commands:

sed

and

perl

always return me : illegal option.

f.e. I tried : sed -i -e 's/OLD/NEW/g' * and i got : " - i illegal option "

Is there any alternative ? I would also be ok with a bash script. This is a productional environment and I cannot install any editor unfortunately.

UPDATE : this works as I want it to:

perl -pi -e 's/OLD/NEW/g' *

But it works only for files in current directory. For all subdirectories it returns me:

Can't do inplace edit: Folder_upgrades is not a regular file. ... ...

But if I move to Folder_upgrades and execute the command, it works.

What should I add to do it recursively, so I run it only once and it will do the work in all files in all sub directories ?

SOLUTION UPDATE: this one works exactly as I wanted, recursively. Although it returns an error about files not being regular, it still does the job.

find . -exec perl -pi -e 's/OLD/NEW/g' '{}' \;
korodani
  • 178
  • 2
  • 18
  • It sounds like you're just passing incorrect options to `sed` and `perl`. What are the *actual* command lines you're trying to run? – larsks Nov 13 '19 at 18:28
  • Are you trying to change the names of the files (eg OLD.c -> NEW.c) or the contents of the files? – mevets Nov 13 '19 at 18:32
  • I want to replace a string in files. I tried : sed -i -e 's/OLD/NEW/g' * and i get : " - i illegal option " – korodani Nov 13 '19 at 19:30
  • You haven't provided enough information to get any kind of an answer. What are the **exact** command or commands that you run are failing? What is the **exact** error message or error messages that you are getting? Post them in your question as text. Do that, and you'll probably get good answers quickly. – Andrew Henle Nov 13 '19 at 19:30
  • 2
    The `-i` option for `sed` is a non-portable, non-standard GNU extension to [the POSIX-standard `sed` utility](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html). See https://stackoverflow.com/questions/3576380/alternative-to-sed-i-on-solaris (Your question is likely a duplicate of that one.) – Andrew Henle Nov 13 '19 at 19:36

1 Answers1

1

You can use code like this:

while read i
do
sed -e 's/OLD/NEW/g' "$i" >/tmp/tmp_file
mv /tmp/tmp_file "$i"
done < (find . -name "*your wildcard for files")

But be careful, this is just a example, if you have space or other special characters in filenames this may not work!!!

Romeo Ninov
  • 6,538
  • 1
  • 22
  • 31