I need to replace a string in a lot of files in a folder, with only ssh
access to the server. How can I do this?
相关问题
- how to split a list into a given number of sub-lis
- Is shmid returned by shmget() unique across proces
- Generate string from integer with arbitrary base i
- how to get running process information in java?
- Converting a string array to a byte array
$ replace "From" "To" -- `find /path/to/folder -type f`
(replace - a string-replacement utility of MySQL Database System)
I found this one from another post (can't remember which) and while not the most elegant, it's simple and as a novice Linux user has given me no trouble
if you have spaces or other special characters use a \
for strings in sub-directories use **
Really lame, but I couldn't get any of the sed commands to work right on OSX, so I did this dumb thing instead:
^- copy these three lines into my clipboard (yes, include the ending newline), then:
vi *
and hold down command-v until it says there's no files left.
Dumb...hacky...effective...
I did concoct my own solution before I found this question (and answers). I searched for different combinations of "replace" "several" and "xml," because that was my application, but did not find this particular one.
My problem: I had spring xml files with data for test cases, containing complex objects. A refactor on the java source code changed a lot of classes and did not apply to the xml data files. In order to save the test cases data, I needed to change all the class names in all the xml files, distributed across several directories. All while saving backup copies of the original xml files (although this was not a must, since version control would save me here).
I was looking for some combination of
find
+sed
, because it worked for me in other situations, but not with several replacements at once.Then I found ask ubuntu response and it helped me build my command line:
And it worked perfectly (well, my case had six different replacements). But please note that it will touch all *.xml files under current directory. Because of that, and if you are accountable to a version control system, you might want to filter first and only pass on to
sed
those actually having the strings you want; like:To maintain my personal English node, I wrote an utility script that help to replace multiple pair of old/new string, for all files under a directory recursively.
The multiple pair of old / new string are managed in a hash map.
The dir can be set via command line or environment variable, the map is hard coded in the script, but you can modify the code to load from a file, if necessary.
It requires bash 4.2, due to some new feature.
en_standardize.sh:
@kev's answer is good, but only affects files in the immediate directory.The example below uses grep to recursively find files. It works for me everytime.