-1

I have a file like this:

W  X  Y  Z
a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4

I want to read each line and then generate result as:

W = a1
X = a2
Y = a3
z = a4
---------------
W = b1
X = b2
Y = b3
z = b4
---------------
W = c1
X = c2
Y = c3
z = c4

I am trying to use nested for loop but it is not working:

for /F "usebackq tokens=* delims= " %%A in ("%file%") do (
echo tushar
call echo %%A

I have made the same in unix using awk and it is working perfectly.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Your code does not feature nested `for` loops, and I see no attempt in trying to solve the task after all; is this really all you have so far? – aschipfl Jul 18 '19 at 09:13
  • I can't see `tushar` in the desired output, so how is the shown code related to your question? – Stephan Jul 18 '19 at 09:17
  • Hi, I got result like this a3 b3 c3 tushar a1 a2 a3 a4 The system cannot find the file . tushar b1 b2 b3 b4................ can you tell me any way to work this out... don't really need for loop... just the output is important. – NoobScripter Jul 18 '19 at 09:22
  • the code you showed doesn't produce this output. Please show your actual code. – Stephan Jul 18 '19 at 09:45
  • `for /F "usebackq tokens=* delims= " %%A in ("%file%") do ( echo tushar call echo %%A for /F "tokens=1 usebackq delims= " %%a in ("%A%") do ( set /A i+=1 call echo %%i%% call set aarray[%%i%%]=%%a call set n1=%%i%% ) set i=0 )` – NoobScripter Jul 18 '19 at 10:45
  • that doesn't produce anything but error messages. Being a good programmer isn't so much being good at coding, but more being good at analyzing the problem and come up with a good logic to solve it. Then the coding itself is (nearly) trivial. See (and analyze) my code below. – Stephan Jul 18 '19 at 12:15

2 Answers2

0

Your problem wasn't only the coding, but also the logic behind it.

Take a look at the code below; I have inserted some comments to show what's being done.

@echo off
setlocal enabledelayedexpansion
set "file=file.txt"

REM read every line...
set line=0
for /f "usebackq delims=" %%A in ("%file%") do (
  set /a lines+=1
  REM ... and tokenize it...
  set count=0
  for %%B in (%%A) do (
    REM ... into an array.
    set /a count+=1
    set "Dta[!lines!-!count!]=%%B"   
  )
)

REM lines now holds the number of lines in the file, count is the number of tokens.
rem set Dta[
REM uncomment above line for troubleshooting (showing the array)

REM for every line [skip first line=Header]
for /l %%l in (2,1,%lines%) do (
  REM for each token in the line
  for /l %%t in (1,1,%count%) do ( 
    REM echo Array[Header-Token] = Array[Line-Token]
    echo !Dta[1-%%t]! = !Dta[%%l-%%t]!
  )
  REM insert delimiter line if it's not the last line:
  if %%l lss %lines% echo -----------
)

See here for a short explanation of delayed expansion. It's easier (and faster) than the call method, you used.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you Stephan, the code works as I needed it to. I made the same code for unix servers and there I was reading the whole file at a time and using `my_array=( $(cat file.txt | awk '{print $1";"$2";"$3";"$4";"$5";"$6";"$7";"$8";"$9";"$10}') )` – NoobScripter Jul 18 '19 at 13:04
  • keep in mind, the history of `cmd` dates back to times, where computers had no disk, a 5.25-inch floppy drive, and a max. memory of 640 kB (not that many people could have afforded that...) and it's primary purpose wasn't to work with text files. So don't expect the same functionality that more "modern" languages have. – Stephan Jul 18 '19 at 13:19
  • Perhaps you should take a look at PowerShell, the official successor of `cmd`, which is a modern, designed language (that actually deserves to be called a language - `cmd` certainly does not) With Powershell, it should be as simple as in Unix. – Stephan Jul 18 '19 at 13:38
0

This is the way I would do it:

@echo off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (test.txt) do (
   set i=1
   for %%b in (%%a) do (
      if not defined header[!i!] (
         set "header[!i!]=%%b"
      ) else (
         for %%i in (!i!) do echo !header[%%i]! = %%b
      )
      set /A i+=1
   )
   echo ---------------
)

If you want to eliminate the additional dash lines, a couple more lines of code are needed...

Aacini
  • 65,180
  • 12
  • 72
  • 108