Home > Blockchain >  How to include a new string with variable on a new column created in a .csv file in batch?
How to include a new string with variable on a new column created in a .csv file in batch?

Time:06-15

I have a .csv like:

location,num1,num2,min,avg,med,max,std,
1344 and 640, 278.000000,8215.000000,0.000000,0.475325,0.465546;0.985196,0.168458,

I want to include on first line the String 'ID' and on second line a variable with the ID as:

ID,location,num1,num2,min,avg,med,max,std,
134085,1344 and 640, 278.000000,8215.000000,0.000000,0.475325,0.465546;0.985196,0.168458,

I don't know how to include the ID variable on the second line. This .csv must be saved with the new result.

CodePudding user response:

There can be used the following batch file code for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F usebackq^ delims^=^ eol^= %%I in ("input.csv") do echo ID,%%I>"output.csv"& goto DataLRows
:DataRows
(for /F usebackq^ skip^=1^ delims^=^ eol^= %%I in ("input.csv") do echo 134085,%%I)>>"output.csv"
rem if exist "output.csv" move /Y "output.csv" "input.csv"
endlocal

The first for /F processes just the first line of the file with name input.csv in current directory. The used for /F options define an empty list of string delimiters and no end of line character to get the entire line in the CSV file of which file name is enclosed in double quotes assigned to the loop variable I without ignoring any non-empty line. This line is output with ID, at the beginning and the output is written into a new file with name output.csv in the current directory.

The command GOTO results in jumping for further processing to the command line below the label DataRows after processing the first line from the file input.csv.

This time the entire for /F command line is enclosed in round brackets to open first the already existing file output.csv and let it open all the time on appending one line after the other from input.csv with skipped first line and 134085, inserted at beginning of each data row output with command ECHO and redirected into the permanently opened file output.csv until all lines are processed.

The last but one line is commented out. Remove the command rem if the created file output.csv should replace the file input.csv in the current directory.

The syntax with specifying the for /F options not with an argument string enclosed in " is unusual, but the only method which exist to define an empty list of string delimiters and no end of line character by using delims= and eol= with eol= at end of the argument string. It is necessary to escape each equal sign and each space with caret character ^ in this case as the options are not enclosed in " to get each space and each equal sign interpreted initially as literal characters by cmd.exe on processing the command line instead of argument string separators as usual before the command FOR interprets the argument string with the options which does not contain anymore any ^.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rem /?
  • setlocal /?

See also:

  • Related