0

So here's the scenario. I'd like to change the following value from true to false in 100's of files in an installation but can't figure out the command and been working on this for a few days now. what i have is a simple script which looks for all instances of a file and stores the results in a file. I'm using this command to find the files I need to modify:

find /directory -type f \ ( -name 'filename' \) > file_instances.txt

Now what i'd like to do is run the following command, or a variation of it, to modify the following value:

sed 's/directoryBrowsingEnabled="false"/directoryBrowsingEnabled="true"/g' $i > $i

When i tested the above command, it had blanked out the file when it attempted to replace the string but if i run the command against a single file, the change is made correctly.

Can someone please shed some light on to this?

Thank you in advance

What has semi-worked for me is the following:

Jotne
  • 40,548
  • 12
  • 51
  • 55
SamiSam
  • 11
  • 2
  • Your question ends quite in a suspensive way... also, your problem does not come from the presence of quotes so you may want to change your title. – PatJ Feb 17 '15 at 15:18

2 Answers2

0

You can call sed with the -i option instead of doing > $i. You can even do a backup of the old file just in case you have a problem by adding a suffix.

sed -e 'command' -i.backup myfile.txt

This will execute command inplace on myfile.txt and save the old file in myfile.txt.backup.

EDIT:
Not using -i may indeed result in blank files, this is because unix doesn't like you to read and write at the same time (it leads to a race condition).

You can convince yourself of this by some simple cat commands:

$ echo "This is a test" > test.txt
$ cat test.txt > test.txt # This will return an error cat being smart
$ cat <test.txt >test.txt # This will blank the file, cat being not that smart
PatJ
  • 5,996
  • 1
  • 31
  • 37
0

On AIX you might be missing the -i option of sed. Sad. You could make a script that moves each file to a tmp file and redirects (with sed) to the original file or try using a here-construction with vi:

cat file_instances.txt | while read file; do
   vi ${file}<<END >/dev/null 2>&1
:1,$ s/directoryBrowsingEnabled="false"/directoryBrowsingEnabled="true"/g
:wq
END
done
Walter A
  • 19,067
  • 2
  • 23
  • 43