Search replace many files sed




















Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Find and replace with sed in directory and sub directories Ask Question. Asked 10 years, 6 months ago. Active 9 months ago. Viewed k times. I run this command to find and replace all occurrences of 'apple' with 'orange' in all files in root of my site: find. What is wrong with this command? Here are some lines of output of find.

Improve this question. And the directory strucuture please. Hm your find is correct, works for me with subdirs. How do you know it does not process subdirectories? Show 4 more comments. Active Oldest Votes. Your find should look like that to avoid sending directory names to sed : find. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.

I have a Visual Studio project, which is developed locally. Code files have to be deployed to a remote server. The only problem are the URLs they contain, which are hard-coded. The project contains URLS such as? I've decided to replace all URLS in my code files with sed before deployment, but I'm stuck on slashes.

I know this is a not a pretty solution, but it's simple and would save me a lot of time. The total number of strings I have to replace is fewer than You can use any character as a delimiter that's not part of either string. Or, you could escape it with a backslash:. You'd want to use the escaped backslash in cases where you don't know what characters might occur in the replacement strings if they are shell variables, for example.

The s command can use any character as a delimiter; whatever character comes after the s is used. I was brought up to use a. Like so:. A common alternative is s foo bar , from which it becomes obvious how to solve your problem. In a system I am developing, the string to be replaced by sed is input text from a user which is stored in a variable and passed to sed. As noted earlier on this post, if the string contained within the sed command block contains the actual delimiter used by sed - then sed terminates on syntax error.

Consider the following example:. Replacing the default delimiter is not a robust solution in my case as I did not want to limit the user from entering specific characters used by sed as the delimiter e. However, escaping any occurrences of the delimiter in the input string would solve the problem. Consider the below solution of systematically escaping the delimiter character in the input string before having it parsed by sed.

Such escaping can be implemented as a replacement using sed itself, this replacement is safe even if the input string contains the delimiter - this is since the input string is not part of the sed command block:. So if you use sed to return some HTML templates on a server , use double backslash instead of single:. Consider the following :. If you do -ie , you create a backup of every file with the letter 'e' appended. A simplier alternative is using AWK as on this answer :.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Often times I need to search and replace a string of text across multiple files in my Linux box. After a bit of research I've come up with a nice solution.

Assuming that you want to search for the string search through multiple files and replace it with replace , this is the one-liner:. Here I'm invoking it with the following parameters:. This is a little command-line utility that takes what receives in input and passes it as argument to another program. So in this example the output of grep is passed to the next command sed as its argument.

In the current snippet I'm using it to replace text with the following parameters:. Both grep and sed support regular expressions, so you can search with grep given a specific pattern and then replace the text with sed given another one. Take a look at the grep manual and the sed manual for more information.

StackOverflow - How to replace a string in multiple files in linux command line. Nope, thanks anyway.



0コメント

  • 1000 / 1000