2

I want to replace all , in a text File with . via Batch.

Actually I just need an alternative for this command

sed.exe "s/,/./g" $clearances.txt$ > $clearance_out.txt$

Problem is sed.exe is not installed on every PC I want to use this on so I plan to read and replace with the standard notepad editor from Windows.

Does anyone know how?

Example for the clearances.txt:

-12,7
-5,6
-7,6
-3,9
Compo
  • 30,301
  • 4
  • 20
  • 32
Mark
  • 23
  • 1
  • 3
  • Batch files cannot interact with graphical user interface. – Squashman May 23 '18 at 12:49
  • 2
    Possible duplicate of [How can you find and replace text in a file using the Windows command-line environment?](https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) – MatSnow May 23 '18 at 13:13

2 Answers2

2

You could use PowerShell instead of sed.

From a batch file:

@PowerShell "(GC .\clearances.txt)|%%{$_ -Replace ',','.'}|SC .\clearances.txt"

At the Command prompt

PowerShell "(GC .\clearances.txt)|%{$_ -Replace ',','.'}|SC .\clearances.txt"
Compo
  • 30,301
  • 4
  • 20
  • 32
  • this is also good. actually it helped me even further for what i initially asked, because like this i can open other filestypes too. thank you – Mark May 23 '18 at 14:50
  • Is there any ability to use this to target the first character of a record in a file? I'm very novice with PowerShell and batch files. – plankton Mar 26 '20 at 16:58
  • 1
    @plankton, as your question is different from that which I answered, you'll need to [create a new question](https://stackoverflow.com/questions/ask). Please note, if you do so, that we expect that you include a [mcve] of your code, _(written to actually perform the task you require of it)_, within your question, complete with an example of the input file content and your required output. – Compo Mar 26 '20 at 17:07
  • Will do. Should I tag you in the post? EDIT: You actually could just answer an existing question I have open - https://stackoverflow.com/questions/60855707/sybase-ase-isql-remove-blank-first-column-from-output-into-csv-file?noredirect=1#comment107673509_60855707 – plankton Mar 26 '20 at 17:12
1

You could do it the following way (using a batch file equivalent for your sed):

Name the file replace_string.bat

@echo off 
    setlocal enableextensions disabledelayedexpansion

    set "search=,"
    set "replace=."

    set "textFile=clearances.txt"

    for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%textFile%" echo(!line:%search%=%replace%!
        endlocal
    )

First we want to make sure the delayed expansion is disabled (for the for magic to work). Next set the variables to search for and in file.

Then in loop I use type to display contents of the file. Then ^ is used to escape & (run first command and then second) and > (redirect).

Then save the loop variable %%i (the double %% is there due to the fact that we are in batch file) into line variable so we could use it. Then we need to enable delayedexpansion so we could use the !...!.

The tricky part is echo(!line:%search%=%replace%!.

echo( normally prints empty line. Here you need to have it for line variable expansion. Without it you would get something like:

'line:' is not recognized as an internal or external command

So we where we have then (thanks to expansion) 12,7:,=. which results into the final result 12.7

tukan
  • 13,399
  • 1
  • 15
  • 39
  • omg it's so complicated. never in a million years i will be willing enough to learn batch i think haha. but it works, thank you very much! – Mark May 23 '18 at 13:43
  • @Mark I'm glad it works for you. it looks complicated at the beginning. This is rather easy some really wild stuff is done by dbenham - https://stackoverflow.com/users/1012053/dbenham. – tukan May 23 '18 at 13:53
  • Let me recommend *not* to name the batch file `replace`, because there is also a command with that name... – aschipfl May 23 '18 at 17:58