Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Use SED for Windowws in batch.  (Read 92694 times)

0 Members and 1 Guest are viewing this topic.

Geek-9pm

    Topic Starter

    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Use SED for Windowws in batch.
« 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. :)

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Use SED for Windowws in batch.
« Reply #1 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.
« Last Edit: January 24, 2014, 08:32:01 AM by Squashman »

briandams



    Beginner

    Thanked: 2
    • Experience: Guru
    • OS: Unknown
    Re: Use SED for Windowws in batch.
    « Reply #2 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.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Use SED for Windowws in batch.
    « Reply #3 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.

    I was trying to dereference Null Pointers before it was cool.

    briandams



      Beginner

      Thanked: 2
      • Experience: Guru
      • OS: Unknown
      Re: Use SED for Windowws in batch.
      « Reply #4 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?

      Geek-9pm

        Topic Starter

        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Use SED for Windowws in batch.
      « Reply #5 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.

      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

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Use SED for Windowws in batch.
      « Reply #6 on: January 25, 2014, 07:10:18 PM »
      And why didn't anybody mention VBScript?   
      Squashman did...
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm

        Topic Starter

        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Use SED for Windowws in batch.
      « Reply #7 on: January 25, 2014, 08:16:13 PM »

      briandams



        Beginner

        Thanked: 2
        • Experience: Guru
        • OS: Unknown
        Re: Use SED for Windowws in batch.
        « Reply #8 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


        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Use SED for Windowws in batch.
        « Reply #9 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.

        Geek-9pm

          Topic Starter

          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Use SED for Windowws in batch.
        « Reply #10 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.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Use SED for Windowws in batch.
        « Reply #11 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.

        Geek-9pm

          Topic Starter

          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Use SED for Windowws in batch.
        « Reply #12 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?  :)

        briandams



          Beginner

          Thanked: 2
          • Experience: Guru
          • OS: Unknown
          Re: Use SED for Windowws in batch.
          « Reply #13 on: January 28, 2014, 05:01:28 AM »
          if you don't want to keep the old file, you can use the -i switch

          Geek-9pm

            Topic Starter

            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: Use SED for Windowws in batch.
          « Reply #14 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.