How do I randomly display a column in one of the lines?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

How do I randomly display a column in one of the lines?

#1 Post by fugitive » 08 Jul 2017 09:14

Hey,good evening everyone,i need your help.
I have a text file with 20 lines. How do I randomly display a column in one of these lines?
and when i press any key,it display all the contents of that row.

(The text has 5 columns per line)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How do I randomly display a column in one of the lines?

#2 Post by aGerman » 08 Jul 2017 13:21

What does "column" mean? How are columns separated in your text file? Or do they have a fixed length?

Steffen

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: How do I randomly display a column in one of the lines?

#3 Post by fugitive » 09 Jul 2017 08:09

aGerman wrote:What does "column" mean? How are columns separated in your text file? Or do they have a fixed length?

Steffen


HI Steffen
The columns separated by the space bar.It looks like this :

Code: Select all

cold      HX   16  aa   bb
water      HQ   16  aa  bc      
safe      HGS\HES\HG6A\HE2   12-16 aa bc
white      HC4A   14    aa   bc
thin      HG3   12    aa   bc
double   HS2   10-16    aa bc
change   HC6   10-16    aa bc
raise      HC5   10-14   aa bc
hot      HB   10 aa bc
forever   HD1   10 aa bc
walk      HA   10-12 aa bc
machine   QF3   10 aa bc

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How do I randomly display a column in one of the lines?

#4 Post by aGerman » 09 Jul 2017 09:46

Okay. In this case that code might be working.

Code: Select all

@echo off &setlocal

set "fname=test.txt"
set "columns=5"

for /f %%i in ('type "%fname%"^|find /c /v ""') do (
  set /a "rndtokens=%random% %% (%%i * columns) + 1, skiplines=rndtokens / columns, cnttokens=rndtokens %% columns"
)
if %cnttokens% equ 0 set /a "skiplines-=1, cnttokens=columns"

<"%fname%" (
  for /l %%i in (1 1 %skiplines%) do set /p "="
  set "line=" &set /p "line="
)

for /f "tokens=%cnttokens%" %%i in ("%line%") do echo %%i
pause
echo %line%

pause


Steffen

fugitive
Posts: 19
Joined: 09 Mar 2017 02:26

Re: How do I randomly display a column in one of the lines?

#5 Post by fugitive » 10 Jul 2017 08:22

aGerman wrote:Okay. In this case that code might be working.
Steffen

Perfect, Steffen! Thanks very much.
would like to tell me why my own batfile can not work ?

Code: Select all

@echo off 
cd /d %~dp0
setlocal enabledelayedexpansion
set file=test.txt
for /f "delims=" %%a in ('type "%file%"') do set /a Line+=1
set /a Line=!random!%%!Line!
if not !Line! equ 0 set skip=skip=!Line!
set /a Line+=1
for /f "%skip% tokens=1-5" %%a in ('type "%file%"') do (
    set /a col=!random!%%5+1
    set C1=%%~a
    set C2=%%~b
    set C3=%%~c
    set C4=%%~d
    set C5=%%~e
    call echo;!Line! line and !col! column: %%C!col!%%
    pause
    echo the line is:
    echo %%~a  %%~b    %%~c    %%~d    %%~e
    goto :End
)
 
:End
pause

Post Reply