Page 1 of 1

Replace a specific character in CSV

Posted: 19 Apr 2021 15:24
by Mahlzeit20
Hi together,

I really love this forum and hope I can find a solution.

I already tried to find something in the search, but I wasn't able to get a solution.

There is a .csv file with a changing order number like 68688-74747-473.
We need to replace the "-" into a "/" - but only in the column 1.

Other columns can also contain the character "-" (e.g. phone number or house number), they don't should be changed.

Maybe I'm too silly for that :oops: I hope someone can help me.

Regards
Thomas

Re: Replace a specific character in CSV

Posted: 19 Apr 2021 16:19
by aGerman
Pretty similar: viewtopic.php?f=3&t=10035

Code: Select all

@if (0)==(0) echo off &setlocal
set "infile=test.csv"
set "outfile=out.csv"
set "separator=;"

cscript //nologo //e:jscript "%~fs0" "%infile%" "%outfile%" "%separator%"
goto :eof @end

var oFSO = new ActiveXObject('Scripting.FileSystemObject'),
    oInFile = oFSO.OpenTextFile(WScript.Arguments(0)),
    oOutFile = oFSO.OpenTextFile(WScript.Arguments(1), 2, true),
    delim = WScript.Arguments(2),
    line = '',
    arr = [],
    idx = 0;

while (!oInFile.AtEndOfStream) {
  line = oInFile.ReadLine();
  arr = line.split(delim);
  arr[0] = arr[0].replace(/-/g, "/");
  oOutFile.WriteLine(arr.join(delim));
}
oInFile.Close();
oOutFile.Close();
Steffen

Re: Replace a specific character in CSV

Posted: 19 Apr 2021 22:51
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion

(for /F "tokens=1* delims=," %%a in (input.txt) do (
   set "col1=%%a"
   echo !col1:-=/!,%%b
)) > output.txt
input.txt

Code: Select all

68688-74747-473, Col-num-2, Col-num-3
88686-47474-374, Num-col-2, Num-col-3
output.txt

Code: Select all

68688/74747/473, Col-num-2, Col-num-3
88686/47474/374, Num-col-2, Num-col-3
Antonio

Re: Replace a specific character in CSV

Posted: 04 May 2021 07:07
by Mahlzeit20
Thank you Steffen and Antonio!!!!! You are the best!