Remove columns from a CSV using Batch Command - windows

I'm trying to create a batch file to delete the first two columns from a csv file
I already tried some codes that I found here on the stack, but they didn't work as I wanted
my input data is like this
Cenário;Tipo de Movimento;Conta;Centro de Custo;Jan;Fev;Mar;Abr;Mai;Jun;Jul;Ago;Set;Out;Nov;Dez;Total
Orçamento;Final;1102345;C01;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;12000
Orçamento;Final;1102346;C01;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;24000
Orçamento;Final;1102347;C05;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;36000
the Output must be like this
Conta;Centro de Custo;Jan;Fev;Mar;Abr;Mai;Jun;Jul;Ago;Set;Out;Nov;Dez;Total
1102345;C01;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;1000;12000
1102346;C01;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;2000;24000
1102347;C05;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;3000;36000
The codes i already tried
CODE 1
#echo off
:main
for /f "tokens=* delims= " %%a in (ORIGINAL.csv) do (
call :sub1 %%a
)
goto :eof
:sub1
for /f "tokens=3* delims=;" %%w in ('type %1') do (
echo.%%w, %%x, %%y, %%z, %%aa, %%ab, %%ac, %%ad, %%ae, %%af, %%ag, %%ah, %%ai
) >> MOD.csv
goto :eof
OUTPUT 1 - Don't give me anything
CODE 2
#Echo off
(for /f "delims=" %%A in (ORIGINAL.csv) do Call :sub1 %%A
) > MOD.csv
goto :Eof
:sub1
Echo(%3',%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15
OUTPUT 2
de',Movimento,Conta,Centro,de,Custo,Jan,Cenário0,Cenário1,Cenário2,Cenário3,Cenário4,Cenário5
1102345',C01,1000,1000,1000,1000,1000,Orçamento0,Orçamento1,Orçamento2,Orçamento3,Orçamento4,Orçamento5
1102346',C01,2000,2000,2000,2000,2000,Orçamento0,Orçamento1,Orçamento2,Orçamento3,Orçamento4,Orçamento5
1102347',C05,3000,3000,3000,3000,3000,Orçamento0,Orçamento1,Orçamento2,Orçamento3,Orçamento4,Orçamento5
CODE 3
#echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%x in (ORIGINAL.csv) do (
set "line=%%x"
for /F "tokens=3-15* delims=;" %%a in ("!line:;=|;!") do (
set "line=%%a%%b%%c%%d%%f"
echo !line:|=!
)) > MOD.csv
OUTPUT 3 - Don't give me anything
I need advices please

Related

Batch variable store result from command

It seems for me a very simple question, however I am struggling a lot with this and don't find an answer yet.
My aim is to count occurences of a specific string in a file.
I do or try to do this using following command:
for /f "tokens=2 delims=;" %%a in ('path to file with searchstring') do (
set count=('path to file I am searching in' find /c "%%a")
if !count! NEQ 0 echo !count!
)
Seems straightforward for me however the second line seems to be wrong as the script exits.
I want to save the result of findin a var because I only wants to outpout Nonzero resutls.
Here's an example batch-file using findstr, for better word matching, (It uses the filenames from your previous question):
#(For /F "UseBackQTokens=2Delims=;" %%G In ("classID.txt")Do #(Set "count=0"
For /F %%H In ('^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
)Do #(Set /A count+=1)&SetLocal EnableDelayedExpansion
If !count! Gtr 0 (Echo %%G: !count!)&Endlocal))&Pause
Here it is fully parenthesized over multiple lines for you to better understand:
#Echo Off
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /I /N "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
Pause
And a fully parenthesized example of it outputting the results to a file named results.txt:
#Echo Off
(
For /F "UseBackQ Tokens=2 Delims=;" %%G In (
"classID.txt"
) Do (
Set "count=0"
For /F %%H In (
'^""%__AppDir__%findstr.exe" /IN "\<%%G\>" "trc.txt"^"'
) Do (
Set /A count +=1
)
SetLocal EnableDelayedExpansion
If !count! Gtr 0 (
Echo %%G: !count!
)
Endlocal
)
)>"results.txt"

How to find string and then divide the token while reading the file line by line using batch script

I have tried to do like this
Inputfile contains the file which has to be read
for /F "tokens=* delims=" %%A in (%INPUTFILE%) do (
SET my_line=%%A
SET my_line=%my_line:#define ACCEL_PRESENT%
findstr /C:"define ACCEL_PRESENT"
if %errorlevel%==0 ( SET my_line=%%A
call :parse_it
)
)
:parse_it
for /F "usebackq tokens=1-6 delims= " %%1 in ('%my_line%') do (
echo %%3 > t.txt
)
I am getting nothing from the above code.Can someone help me with the desired results
Here is one possible solution:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%A in (%INPUTFILE%) do (
set "my_line=%%A"
set "my_line=!my_line:#define ACCEL_PRESENT=!"
echo !my_line! | findstr /C:"define ACCEL_PRESENT"
IF !errorlevel! EQU 0 ( SET "my_line=%%A" && call:parse_it )
)
:parse_it
for /F "usebackq tokens=1-6 delims= " %%A in ("%my_line%") do echo %%C > t.txt
rem exit /b
I hope you wanted to delete string #define ACCEL_PRESENT.
Also, say me if %my_line% is a command (I don't think so, so I removed single-quotes and I added double-quotes).
Better to use letter variables in for loops!

Is there any way to remove the entries with same process names?

I'm trying to get the top 10 memory consuming processes and I am using the code provided as an answer in this Question. It works fine except that I don't want multiple entries of processes consuming different memory size and I was unable to sort it because it's memory consumption is different.
The code is:
#echo off
setlocal EnableDelayedExpansion
(for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size= %%b"
echo !size:~-10!:%%a
)) > wmicc.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmicc.txt') do (
echo %%a
set /A i+=1
if !i! equ 25 goto :end
)
:end
Can I get the output as:
96931840:iexplore.exe
82161664:explorer.exe
42319872:svchost.exe
31469568:dwm.exe
25690112:SearchIndexer.exe
17002496:taskhostex.exe
11007590:VCExpress.exe
8033894:avp.exe
7190528 :Skype.exe
7000416 :SkypeBrowserHost.exe
Instead of the output from existing code:
96931840:iexplore.exe
82161664:explorer.exe
42319872:svchost.exe
33656832:svchost.exe
31469568:dwm.exe
26943488:iexplore.exe
25690112:SearchIndexer.exe
18550784:svchost.exe
17002496:taskhostex.exe
16343040:svchost.exe
#echo off
setlocal EnableDelayedExpansion
set num=25
set /A skip=-num
for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size= %%b"
set "size=!size:~-10!"
if not defined proc[%%a] (
set "proc[%%a]=!size!"
set "size[!size!:%%a]=1"
set /A skip+=1
) else if %%b gtr !proc[%%a]! (
set "size[!proc[%%a]!:%%a]="
set "proc[%%a]=!size!"
set "size[!size!:%%a]=1"
)
)
if %skip% lss 1 (set "skip=") else set "skip=skip=%skip%"
for /F "%skip% tokens=2 delims=[]" %%a in ('set size[') do echo %%a
Output example:
5517312:NisSrv.exe
5537792:HD-LogRotatorService.exe
5939200:spoolsv.exe
6062080:WUDFHost.exe
6168576:igfxpers.exe
6279168:conhost.exe
6369280:wmpnetwk.exe
6533120:hkcmd.exe
6692864:igfxtray.exe
8073216:notepad.exe
8077312:WMIC.exe
8105984:taskhost.exe
9306112:lsass.exe
11157504:winlogon.exe
11767808:taskhostex.exe
13402112:SkyDrive.exe
16629760:TabTip.exe
17121280:SearchIndexer.exe
26120192:HD-Agent.exe
32325632:csrss.exe
36429824:svchost.exe
43114496:dwm.exe
101998592:explorer.exe
110260224:MsMpEng.exe
114425856:chrome.exe
#echo off
setlocal EnableDelayedExpansion
for /f "delims==" %%a in ('set # 2^>nul') do set "%%a="
(for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size= %%b"
echo !size:~-10!:%%a
)) > wmicc.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmicc.txt') do (
for /f "tokens=1*" %%p in ("%%a") do (
rem the following line doesn't work
rem if not defined "#%%q"
rem the preceding line doesn't work
SET "flag="
FOR /f "tokens=1*delims==" %%v IN ('set #%%q 2^>nul') DO IF /i "%%v"=="#%%q" SET "flag=Y"
IF NOT DEFINED flag (
set "#%%q=Y"
echo %%a
set /A i+=1
if !i! equ 25 goto :end
)
)
)
:end
This tokenises the entire line as read into %%p,q so %%q contains the process-name. It then checks whether the variable #processname is defined and if it is not, generates the report-line and sets #processname so that the first mention will be the last as that variable is now defined.
The first line is designed to set any existing #* variables to empty; the 2^>nul suppresses error messages should no #* variables exist and the caret escapes the > to tell cmd that the > is part of the command to be executed, not of the for.
[later]
It appears that the if defined "varname" that I invented doesn't actually work, although it appeared to work when I tested it. I've replaced that instruction with code that does work and marked it out.

How to use Windows batch for bicirculation

two files 1.txt 2.txt
1.txt
a2441
b4321
p8763
2.txt
Apple
banana
peach
I want to generated content in 12.bat
ren a2441 Apple
ren b4321 banana
ren p8763 peach
I try to write like this but failed
#echo on
for /f %%a in (1.txt) do (
for /f %%b in (2.txt ) do (
ren %%a %%b>>12.bat
)
)
pause
How can I achieve my desired results? help me,thks
It is not so straightforward. You'll need to skip increasing amount of lines in the inner loop , but because of the FOR parsing you'll need additional subroutine call.And if there are empty lined they need to be stripped with findstr for more accurate skipping of the lines. Also skip argument in FOR does not accept 0 so also an if is needed.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set /a s=0
for /f "tokens=* delims=" %%a in (1.txt) do (
call :inner !s!
echo ren %%a !second!
set /a s=s+1
)
endlocal
pause
exit /b
:inner
set "second="
if %1==0 (
for /f "tokens=* delims=" %%b in ('findstr /r "[a-zA-Z]" "2.txt"') do (
if not defined second set "second=%%b"
)
) else (
for /f "skip=%1 tokens=* delims=" %%b in ('findstr /r "[a-zA-Z]" "2.txt"' ) do (
if not defined second set "second=%%b"
)
)
The problem with anidated loops is that for each line of the outer file/loop all the lines of the inner file/loop are processed.
One simple solution is to number the lines of both files and only generate the output line when both numbers match. Quick and dirty:
#echo off
setlocal enableextensions disabledelayedexpansion
>"12.bat" (
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" 1.txt') do (
for /f "tokens=1,* delims=:" %%c in ('findstr /n "^" 2.txt') do (
if %%a==%%c (
echo ren %%b %%d
)
))
)
This code just uses findstr to generate line numbers (/n) for each of the input files, and using the delims and tokens clauses of the for /f command to separate them from the original line contents (note: if the lines could start with a colon, more code is needed). If the line number in first file matches the line number in second file the command is echoed (there is a active output redirection to the final file)
Another option is to assign each file to a input stream and to read them using set /p operations from each of the streams
#echo off
setlocal enableextensions disabledelayedexpansion
call :processFiles 9<"1.txt" 8<"2.txt" >"12.bat"
goto :eof
:processFiles
<&9 set /p f1= || goto :eof
<&8 set /p f2= || goto :eof
echo ren %f1% %f2%
goto :processFiles
In the call to the subroutine we assign each file to a stream. Inside the subroutine each stream is read with set /p (note: set /p can not read lines longer than 1021 characters)

read then write delimited file - Batch cmd

Hi I have a pipe | delimited file.
I need to reverse all the numbers in there
The file looks like this
Entity|Division|Channel|200|300|800
I need to read the file an create a new one with reversed numbers
Entity|Division|Channel|-200|-300|-800
trying to make this work but, not entirely sure how to amend the text I'm getting. I need help on the :processToken procedure. How to output the tokens into a new file and add "-" and add back the delimiter |
for /f "tokens=* delims= " %%f in (M:\GPAR\Dev\EQ\Upload_Files\eq_test.txt) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=* delims=|" %%a in ("%line%") do (
)
if not "%line%" == "" goto :processToken
goto :eof
Thanks
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6 delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
Should work if your file contains only lines like the one you've posted.
EDIT output the part after the 6th token:
#echo off
set "ent_file=c:\entity.txt"
break>"%tmp%\rev.txt"
for /f "usebackq tokens=1,2,3,4,5,6,* delims=|" %%a in ("%ent_file%") do (
echo %%a^|%%b^|%%c^|-%%d^|-%%e^|-%%f|%%g
)>>"%tmp%\rev.txt"
move /y "%tmp%\rev.txt" "%ent_file%"
del /q /f "%tmp%\rev.txt"
#echo off
setlocal EnableDelayedExpansion
set digits=0123456789
(for /F "delims=" %%f in (eq_test.txt) do (
set "input=%%f"
set "output="
call :processToken
set /P "=!output:~1!" < NUL
echo/
)) > new_file.txt
goto :EOF
:processToken
for /F "tokens=1* delims=|" %%a in ("%input%") do (
set "token=%%a"
set "input=%%b"
)
if "!digits:%token:~0,1%=!" neq "%digits%" set "token=-%token%"
set "output=%output%|%token%"
if defined input goto processToken
exit /B

Resources