| 1. |
Solve : batch program + Find and replace :(? |
|
Answer» I have file which has some lines.. e.g. I need a batch code which will search "config.vm.box" in file and replace next string "test_15_4" with %replace%.. This should replace string on only line number 21. What restrictions on tools do you have? Any tool available in Windows 7 home+ for example?I did not understand your qs. I have one batfile which does something. I want to include new code in this bat file only. What new code should do is , to go to some file(name i will provide) , find config.vm.box and replace certain string from the same line. Quote from: pawantlor on June 03, 2015, 04:37:56 AM I did not understand your qs. This task can be done a number of different ways: some ways will fail when non-latin or 'poison' characters or unicode characters are in the file, or the filepath/name to be processed, and some ways are very robust or require less code. So my question was to find out which version of windows you are using (I should have been clearer there) and also if you can use windows scripting host or other standard tools that comes with Windows. I asked because there are times that people ask for a solution, and then they decide that they want a pure vanilla and basic script solution, when someone has already used their time to write some code. My system is windows 7 professional. I understood ur concern. File may not have Latin characters but can have all characters which can be typed using SIMPLE keyboard. And yes file has some file paths. Example I gave has few chars and path, so give me code like that. Let me know if you need more info, I can provide you file if required.I locked your other thread. Please do not ask the same question more than once. Thank you.Anyone for solution?As was mentioned, many tools do not allow chars outside of the normal ASCII range and even some ASCII chars can not be used. A very generic find and replace program would have to be written in C or some language that allows a low-level substitution. But as mentioned, your Windows 7 pro already has some tools available. Notably Powershell is now provided in most version of Windows to give administrators a tool that can be used at the command kine. Sort of a batch extension. There are so many variations, it is hard-to give a short simple example. Here is one that might work: http://weblogs.asp.net/efrancobisi/a-simple-powershell-script-to-find-and-replace-using-regular-expressions-in-multiple-files Or maybe this: http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/use-powershell-to-replace-text-in-strings.aspx From these examples you can see that some kind of explanation has to be given about the objective. If there are no forbidden chars, one can use a simple batch file. http://www.dostips.com/?t=batch.findandreplace And there is an utility called SED that has been imported from UNIX to MS Windows. See link below. http://www.grymoire.com/Unix/Sed.html The problem is that most tools require some kind of escape char to delimit the text you want to find and replace. Which means you would have to know ahead of time which chars would not be in the file. Even a utility that allows your to SPECIFY the escape char, you still have to know ahead of time if that char is not in the file. Honest. nobody wants to confuse you. The issue is to outline a clear, concise objective and identify potential issues. THANKS geek for your detailed explanation. I know that there are different tool which will Have easy way to do this.. But problem is I am 90% done with my utility in batch, so I cannot move back and change tool. I am just remaining with this stuff.. If you have simple solution to this in bat irrespective of chars to parse.. Please provide. I will try to remove all special characters/paths from the Input file in which I need to replace string and keep input file simple.Did not mean to say you have to abandon you rcode. A batch file can call another program and then return to where it left off. As you already know, the %variable% trick can be used in batch file to pass something on to another batch or program. The quick way, for me, is to put one line of SED into your batch. Code: [Select]SED 's/regexp/replacement/g' inputFileName > outputFileNameHere is a simple explanation of SED. Quote sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language. sed was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems.http://en.wikipedia.org/wiki/Sed I would write another batch that invokes SED and then returns to the caller. SEDJOB.BAT Code: [Select]sed 's/%find%/%replace%/%input_file% >output.tmp SED does no do an output file, so you have to have the redirect to do it. The find and replace are the variables you made earlier. Likewise with input_file You will have to copy the output to the original file. Find a suitable version of SED here: http://sourceforge.net/projects/gnuwin32/ That is the best I can do now. I have to go finish mopping the floor before the wife comes home. If anybody sees I made a mistake, please correct me. I have to go now.How to resolve with SED? Any idea? Wow... Quote from: pawantlor on June 03, 2015, 12:01:05 PM Any idea? Some person wrote this - it applies to you in this situation too: Quote The paragraphs below outline ways to get a script that works, and quickly.foxdrive.. I did not understand you.. What do you have to say I am not respectful.. I am just a beginner.. I don't have much knowledge about stuff.. But what I know is I don't hurt anyone with just asking question. I have replied to geeks reply, as he suggested to use sed. And I don't know sed, so I asked how to solve with sed.. I appreciate each one of you as you guys are the one who can solve my problem.. That is the reason I asked my qs to people who are mastermind.. I hope you understand and provide me solution.. i tried this way but some issue i guess.. for /F "USEBACKQ delims=" %%j in ("%textfile%") do ( set row=%%j IF "!row:config.vm.box=!"=="%%j" ( echo %%j>>"%outputFile%") ELSE ( echo %%j>>Nul && ECHO config.vm.box = "%BoxName%">>"%outputFile%") PAUSE ) |
|