Solved Print number of columns based on input paramter

May 23, 2019 at 02:33:44
Specs: DOS
in the below script I want the number of tokens to be passed from the script

for ex script name is cut_cols.bat

FOR /f "tokens=1-5 delims=," %%a IN (input_file) DO ECHO %%a,%%b,%%c,%%d,%%e>>output_file


I want to run the script with input parameter cut_cols.bat 6.

it should automatically make tokens 1-6 and echo a to f

FOR /f "tokens=1-6 delims=," %%a IN (input_file) DO ECHO %%a,%%b,%%c,%%d,%%e%%f>>output_file

Thank you!!


See More: Print number of columns based on input paramter

Report •

✔ Best Answer
May 28, 2019 at 14:45:11
I, as always, go for the minimalist approach.
SETLOCAL EnableDelayedExpansion
SET tokens= %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z
SET /A end=%1 * 3 - 1
SET tokens=!tokens:~1,%end%!
>output_file (FOR /F "TOKENS=1-25* DELIMS=," %%a IN (input_file) DO @ECHO.%tokens%
)

How To Ask Questions The Smart Way



#1
May 23, 2019 at 07:08:58
start the file like so:

cut_cols.bat 6

This might cause issues if you only want column 1


cut_cols.bat:

@echo off
if "%1"=="" echo no parameter found&pause>nul&exit/b
FOR /f "tokens=1-%1 delims=," %%a IN (input_file) DO ECHO %%a,%%b,%%c,%%d,%%e%%f>>output_file

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Report •

#2
May 23, 2019 at 21:53:40
Here's a hint for what I suspect is the correct answer, hidde663:
SET tokens= %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z

Not sure about the number of percent signs; might have to double them up.

How To Ask Questions The Smart Way


Report •

#3
May 27, 2019 at 08:25:47
Didnt get that far last time i tried, will do another attempt tonight or tommorow maby.
I remember getting to annoyed when trying to format the output, but i guess a double for /f loop would fix that.

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Report •

Related Solutions

#4
May 27, 2019 at 11:44:37
Sounds like you're overthinking it, but I bet whatever you come up with will be interesting.

How To Ask Questions The Smart Way


Report •

#5
May 27, 2019 at 21:46:39
As usual, sample data is "rare to none". Also, how to handle lines with less than the specified number of delimited columns: add null commas or truncate? Are there spaces in the data? Is the data quoted? For batch, at least, these considerations can be critical. Absence of sample data, and expected output, leaves me in the dark. Here's my take fwiw:

@echo off & setlocal
set x=%1
if not defined x goto :eof
for /f "tokens=*" %%a in (test) do (
set c=0
set out=
call :xx %%a
)
goto :eof

:xx
set out=%out%%1%,
set /a c+=1
if %c% gtr %x% goto :xit
shift
rem here's where you can pad commas or not:
if "%1" equ "" goto :xit
rem disable that line to pad commas to the input spec
goto :xx

:xit
echo FINAL OUT: %out:~0,-1%

null commas will fail.

message edited by nbrane


Report •

#6
May 28, 2019 at 14:45:11
✔ Best Answer
I, as always, go for the minimalist approach.
SETLOCAL EnableDelayedExpansion
SET tokens= %%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,%%r,%%s,%%t,%%u,%%v,%%w,%%x,%%y,%%z
SET /A end=%1 * 3 - 1
SET tokens=!tokens:~1,%end%!
>output_file (FOR /F "TOKENS=1-25* DELIMS=," %%a IN (input_file) DO @ECHO.%tokens%
)

How To Ask Questions The Smart Way


Report •

#7
May 29, 2019 at 01:01:46
RE #6 Damn, didnt even know this was pissible "DO @ECHO.%tokens%"

I ended up getting %%a,%%b,%%c,%%d,%%e,%%f,%%g as output when giving parameter 6, but the variables were not resolved... Kinda frustrating.

Anyways, very very nice nbrane & Razor2.3

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Report •

#8
May 29, 2019 at 03:05:50
#6 BRILLIANT

It worked perfect in all tests. Thanks a lot Razor2.3

and always appreciate your help hidde663 & nbrane

Thanks a lot guys for all your help. Cheers!


Report •

Ask Question