

InterviewSolution
1. |
Solve : Use SED for Windowws in batch.? |
Answer» Often batch programmers want to have a batch that will find and replace things in a text file. This can be called 'string substitution'. Of course you can do that in NOTEPAD, but not from the command line. SED runs from a command and can be inside batch file. When done, SED goes back to the batch file. @ECHO offCopied from: http://www.thoughtasylum.com/blog/2011/9/30/using-sed-on-windows.html Download free: http://www.freedownloadaday.com/2008/01/09/sed-for-windows/ Once you GET used to the syntax, it is easy to modify any text file. You save the output to a new file. There are a few more native options to Windows for Find and Replace. Pure batch. http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace Surprised you didn't mention PowerShell. I have seen you mention it a lot on the forums over the years. You can call out to a power shell script within a batch file Code: [Select]Get-Content test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt Hybrid Jscript/batch http://www.dostips.com/forum/viewtopic.php?f=3&t=3855 http://www.dostips.com/forum/viewtopic.php?f=3&t=4697 And VBscript has decent find and replace options as well. You could again build the vbscript on the fly and use it within the batch file.Better to get the GNU version of sed for windows. Talking about replacement, many tools can be used, including Perl Code: [Select]perl -pe 's/abc/def/' myFile.txt Similar syntax, accept Perl does a lot more. Even not with Perl, awk is also a better suited tool to use than sed, because awk is a nice little programming language. There's no point learning sed nowadays because what sed can do, awk can do, and it does more as well.Quote from: briandams on January 25, 2014, 10:51:53 AM Better to get the GNU version of sed for windows.The link in the original post links to a site which itself links directly to the GNU download for sed. Quote from: Squashman on January 24, 2014, 08:09:06 AM There are a few more native options to Windows for Find and Replace.any other caveats in using it beside the ones documented?Yes Pearl, awk and Powershell are all tools that can be used. For some, learning SED may be more easy. Ar lest for simple find and replace. Here is thread about some things ported from Unix for use in Windows: http://stackoverflow.com/questions/7866512/shell-with-grep-sed-awk-in-windows As can be seen, it gets to be MENTAL overload. Sed, Ted, ask, squeak, , feed the bird. And why didn't anybody mention VBScript? Download for SED for Windows HERE. BTW: There was an line editor in DOS that can be used if you do a hot patch on the code. But that is said to be a 'hack' that violates some kind of law. DOS edlin Quote from: Geek-9pm on January 25, 2014, 06:14:38 PM And why didn't anybody mention VBScript?Squashman did...Quote from: BC_Programmer on January 25, 2014, 07:10:18 PM Squashman did...Oops. Missed it. Quote from: Geek-9pm on January 23, 2014, 10:51:45 PM @ECHO off Code: [Select]awk "/ERROR/{ print NR\"\t\"$0 }" myFile.txt Geek, Why not just make sure SED is in your PATH instead of doing two commands to get to the path where it is installed. Why bother changing the directory where sed is installed. Either use the CD /d OPTION or pushd if you want to set the working directory to where sed is installed. Or just spell out the whole path to the executable. I always prefer to set my working directory to where my input files are located.Quote from: Squashman on January 25, 2014, 11:08:14 PM Geek,Right. Much better to put it in the PATH, or else install it in a directory already is in the PATH. The installer I used only set the PATH for the current user, not all users. Quote from: Geek-9pm on January 25, 2014, 11:38:33 PM Right.So your example above is incorrect? Last time I checked, the program files folder was available to all users.SED is to be used as a command line utility. You have to open a CMD box, or 'DOS' box to run it. In the DOS box type PATH to see the path used for DOS commands. All such command line utilities have to be somewhere that can be found in the PATH used for commands in the command mode. This applies to all command line utilizes that are not GUI things. . Programs installed in the program directory are not available to the CMD or DOS box. With some exceptions. Presently I moved SED over to D:\gnu32\bin and put that at the end of the PATH. Yhat way I don't have to use the full path. This first example I gave was far to complex. Here is is super simple A file named OLD.TXT has: My name is ABC Jones, I line at 123 Main. ...now give the command: sed s/abc/xyz/ <OLD.TXT >NEW.TXT OR sed s/123/789/ <OLD.TXT >NEW.TXT Look at the NEW.TXT file. Is that simple? if you don't want to keep the old file, you can use the -i switchQuote from: briandams on January 28, 2014, 05:01:28 AM if you don't want to keep the old file, you can use the -i switchRight. The horrible thing about sed is the documentation goes on and on and on... So I wanted to give the very simple form that anybody can use without reading the manual. Again, this is about the version of sed that works in Windows. |
|