Computer Hope

Software => Computer programming => Topic started by: Geek-9pm on January 23, 2014, 10:51:45 PM

Title: Use SED for Windowws in batch.
Post by: Geek-9pm on January 23, 2014, 10:51:45 PM
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.

SED is not included in Windows because it is really a Unix program. But it was ported to Windows so time ago and works very well.

The command line syntax is basically like this:
SED s/abc/xyz/g filename

That means substitute xyz with abc for the whole file.
Output is displayed.
Well, yeah, there are a few more details. But the above is the idea.
Quote
@ECHO off
ECHO Set the current directory to the folder in which SED was installed
C:
CD "C:\Program Files\GnuWin32\bin"
ECHO Add line numbers
SED = "C:\temp\original.log" > "C:\temp1.log"
ECHO Format line numbers
SED "N;s/\n/\t/" "C:\temp\temp1.log" > "C:\temp\temp2.log"
ECHO Output only the lines containing the word 'ERROR'
SED -n "/ERROR/p" "C:\temp\temp2.log" > "C:\temp\processed.log"
ECHO Remove temporary files
DEL "C:\temp\temp1.log"
DEL "C:\temp\temp2.log"
Copied 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. :)
Title: Re: Use SED for Windowws in batch.
Post by: Squashman on January 24, 2014, 08:09:06 AM
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.
Title: Re: Use SED for Windowws in batch.
Post by: briandams on January 25, 2014, 10:51:53 AM
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.
Title: Re: Use SED for Windowws in batch.
Post by: BC_Programmer on January 25, 2014, 12:02:16 PM
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.

Title: Re: Use SED for Windowws in batch.
Post by: briandams on January 25, 2014, 05:25:03 PM
There are a few more native options to Windows for Find and Replace.

Pure batch.
http://www.dostips.com/DtCodeBatchFiles.php#Batch.FindAndReplace

any other caveats in using it beside the ones documented?
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on January 25, 2014, 06:14:38 PM
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. (http://gnuwin32.sourceforge.net/packages/sed.htm)

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  (http://www.computerhope.com/edlin.htm)
Title: Re: Use SED for Windowws in batch.
Post by: BC_Programmer on January 25, 2014, 07:10:18 PM
And why didn't anybody mention VBScript?   
Squashman did...
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on January 25, 2014, 08:16:13 PM
Squashman did...
Oops. Missed it.  :-[
Title: Re: Use SED for Windowws in batch.
Post by: briandams on January 25, 2014, 10:03:34 PM
@ECHO off
ECHO Set the current directory to the folder in which SED was installed
C:
CD "C:\Program Files\GnuWin32\bin"
ECHO Add line numbers
SED = "C:\temp\original.log" > "C:\temp1.log"
ECHO Format line numbers
SED "N;s/\n/\t/" "C:\temp\temp1.log" > "C:\temp\temp2.log"
ECHO Output only the lines containing the word 'ERROR'
SED -n "/ERROR/p" "C:\temp\temp2.log" > "C:\temp\processed.log"
ECHO Remove temporary files
DEL "C:\temp\temp1.log"
DEL "C:\temp\temp2.log"

Code: [Select]
awk "/ERROR/{ print NR\"\t\"$0 }" myFile.txt

Title: Re: Use SED for Windowws in batch.
Post by: Squashman on January 25, 2014, 11:08:14 PM
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.
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on January 25, 2014, 11:38:33 PM
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.
...
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.
Title: Re: Use SED for Windowws in batch.
Post by: Squashman on January 26, 2014, 06:23:35 AM
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.
So your example above is incorrect? Last time I checked, the program files folder was available to all users.
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on January 27, 2014, 06:40:02 PM
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?  :)
Title: Re: Use SED for Windowws in batch.
Post by: briandams on January 28, 2014, 05:01:28 AM
if you don't want to keep the old file, you can use the -i switch
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on January 28, 2014, 08:33:04 PM
if you don't want to keep the old file, you can use the -i switch
Right.
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.
Title: Re: Use SED for Windowws in batch.
Post by: briandams on January 29, 2014, 05:31:49 AM
I prefer to cut down on learning curve of  using too many disparate tools, and stick to one language, with a lot of available APIs. Strawberry Perl Portable (http://strawberryperl.com/releases.html). No installation needed. Just one directory full of goodies.
Title: Re: Use SED for Windowws in batch.
Post by: Geek-9pm on May 29, 2017, 03:02:14 PM
ERRATA
This is rather late, but somehow it was not mentioned t in Windows you can not use the simple quote thin '
Instead  you have to us the double quote thing "

Also some versions of sed want  a g after the last /
so---
 sed s/123/789/ <OLD.TXT  >NEW.TXTBE
BE--
  sed "s/123/789/G" <OLD.TXT  >NEW.TXTBE

sorry  :-[

For more about sed, see the archives:
https://www.computerhope.com/unix/used.htm