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
If the file contains backslashes (paths usually) you can try something like this:
ex:
There are a few standard answers to this already listed. Generally, you can use find to recursively list the files and then do the operations with sed or perl.
For most quick uses, you may find the command rpl is much easier to remember. Here is replacement (foo -> bar), recursively on all files:
rpl -R foo bar .
You'll probably need to install it (
apt-get install rpl
or similar).However, for tougher jobs that involve regular expressions and back substitution, or file renames as well as search-and-replace, the most general and powerful tool I'm aware of is repren, a small Python script I wrote a while back for some thornier renaming and refactoring tasks. The reasons you might prefer it are:
To use it,
pip install repren
. Check the README for examples.To replace a string in multiple files you can use:
E.g.
Source blog
To replace a path within files (avoiding escape characters) you may use the following command:
The @ sign means that all of the special characters should be ignored in a following string.
Below command can be used to first search the files and replace the files
find . |xargs grep search string | sed 's/search string/new string/g'
e.g find . |xargs grep abc | sed 's/abc/xyz/g'
The first line occurrences of "foo" will be replaced with "bar". And you can using the second line to check.