0
votes

I have a .csv file which looks like this:

...
;OUTPUT;DISCRETE;VOLTAGE1;;
;OUTPUT;DISCRETE;VOLTAGE2;;
...

Now I want to search this .csv file for the string "VOLTAGE1" and if found, write "#" to the beginning of the line where the search string was found.

So the .csv file should look like this after the batch script finishes:

...
#;OUTPUT;DISCRETE;VOLTAGE1;;
;OUTPUT;DISCRETE;VOLTAGE2;;
...

I already found the way how to search for a string in a file but I don't know how I can write "#" to the beginning of the line in the "do" part of the for loop. So how can I do this?

My code so far:

@echo off
setlocal
for /F "tokens=1* delims=;" %%a in ('findstr /I "VOLTAGE1" file.csv') do <write # to beginning of line>
endlocal
1

1 Answers

0
votes

Is powershell an option? e.g.:

ipcsv -d ';' -h a,b,c,d,e,f -pa infile.csv          |
% { if ($_.d -like "voltage1") { $_.a = "#" }; $_ } |
ConvertTo-Csv -d ';' -nti | select -skip 1          |
% { $_ -replace '"','' }

Or ungolfed:

Import-Csv -Delimiter ';' -Header a,b,c,d,e,f -Path infile.csv  |
ForEach-Object { if ($_.d -like "voltage1") { $_.a = "#" } $_ } |
ConvertTo-Csv -Delimiter ';' -NoTypeInformation                 |
Select-Object -skip 1                                           |
ForEach-Object { $_ -replace '"','' }

Output:

#;OUTPUT;DISCRETE;VOLTAGE1;;
;OUTPUT;DISCRETE;VOLTAGE2;;