hi to all,
i need help rewriting a test file in a specific way. as i am an amateur, i need your help. the starting file is file.txt like this:
2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
first part is date/time and is ok
0 is a fixed value in second position that i need to change in a new fixed value (ex. 1132)
after this, in third position i need to add a fixed value that is alwais 2
in 4th position i need the value of the 6th position on the original file
in 5th position i need a fixed value that is alwais 0
the result i need is a new txt file like this:
2022-11-19 10:06:01;1132;2;8.8556;0
thank to all!!!!
rewrite a txt file
Moderator: DosItHelp
Re: rewrite a txt file
i resolve the problem... but i need last help!!!!
here the script:
@echo
cd C:\polaris\test
ren *.csv file.txt
set "txt=C:\polaris\test\file.txt"
more +2 "%txt%" >"%txt%.new"
move /y "%txt%.new" "%txt%" >nul
for /f "tokens=1,2,3,4,5,6,7* usebackq delims=," %%a in ("C:\polaris\test\file.txt") do echo %%a;3100;2;%%f;0;>>C:\polaris\test\file2.txt
input:
xxxxxxxxxxxx
xxxxxxxxxxxx
2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
output:
2022-11-19 10:06:01;3100;2;8.8556;0;
i need to remove the ; after the last 0
right output:
2022-11-19 10:06:01;3100;2;8.8556;0
here the script:
@echo
cd C:\polaris\test
ren *.csv file.txt
set "txt=C:\polaris\test\file.txt"
more +2 "%txt%" >"%txt%.new"
move /y "%txt%.new" "%txt%" >nul
for /f "tokens=1,2,3,4,5,6,7* usebackq delims=," %%a in ("C:\polaris\test\file.txt") do echo %%a;3100;2;%%f;0;>>C:\polaris\test\file2.txt
input:
xxxxxxxxxxxx
xxxxxxxxxxxx
2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
output:
2022-11-19 10:06:01;3100;2;8.8556;0;
i need to remove the ; after the last 0
right output:
2022-11-19 10:06:01;3100;2;8.8556;0
Re: rewrite a txt file
FOR /F is your friend (type HELP FOR from the command prompt):
In your file.txt you have:
Put this into a .bat/.cmd file:
Output looks like this:
So you see output is exactly as your input. Rearrange variables %%f ... %%l to suit your needs. %%f represents first field (delimited by comma), %%g is the second field etc.
So your code would look like this:
Result:
Note that I changed comma for a semicolon!
Saso
In your file.txt you have:
Code: Select all
2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
Code: Select all
@echo off
REM fields: f g h i j k l (note:small letter L)
REM 2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
REM so we have 7 letters (from 'f' to 'l'. So we need 7 tokens)
for /f "tokens=1-7 delims=," %%f in (file.txt) do echo %%f,%%g,%%h,%%i,%%j,%%k,%%l
Code: Select all
2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
So your code would look like this:
Code: Select all
@echo off
REM fields: f g h i j k l (note:small letter L)
REM 2022-11-19 10:06:01,0,17.14,68.27,12.9,8.8556,10.5928
REM so we have 7 letters (from 'f' to 'l'. So we need 7 tokens)
REM here we create an empty file
break>new_file.txt
REM note: now commas (,) are replaced by semicolons (;) as per your post
for /f "tokens=1-7 delims=," %%f in (file.txt) do (echo %%f;1132;2;%%k;0)>>new_file.txt
Code: Select all
2022-11-19 10:06:01;1132;2;8.8556;0
Saso
Re: rewrite a txt file
thanks Saso,
i try to do it in my way:
for /f "tokens=1,2,3,4,5,6,7* usebackq delims=," %%a in ("C:\polaris\test\file.txt") do echo %%a,3100,2,%%f,0;>>C:\polaris\test\file2.txt
but i had a ; at the end
but your solution is better (there is no ; at the end)
i try to do it in my way:
for /f "tokens=1,2,3,4,5,6,7* usebackq delims=," %%a in ("C:\polaris\test\file.txt") do echo %%a,3100,2,%%f,0;>>C:\polaris\test\file2.txt
but i had a ; at the end
but your solution is better (there is no ; at the end)
Re: rewrite a txt file
You do not need the ; if you place the redirection to file at the start of the line.
My guess is that you added the ; to get the redirection working.
A special feature of redirection is that a digit immediately preceding the redirection becomes part of the redirection and will not be send as redirected output. This digit represents the type of output, with the default value of 1, meaning normal text output.
Code: Select all
>>C:\polaris\test\file2.txt for /f "tokens=1,2,3,4,5,6,7* usebackq delims=," %%a in ("C:\polaris\test\file.txt") do echo %%a,3100,2,%%f,0
A special feature of redirection is that a digit immediately preceding the redirection becomes part of the redirection and will not be send as redirected output. This digit represents the type of output, with the default value of 1, meaning normal text output.