Put columns on lines [Mettre des colonnes sur des lignes]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
alid0381
Posts: 28
Joined: 09 May 2012 10:37

Put columns on lines [Mettre des colonnes sur des lignes]

#1 Post by alid0381 » 21 Jan 2014 06:28

Hello everybody,

I have a text file "CLOLG.txt" that contains 4 Ligns :

Ligne 1....
Ligne 2....
Ligne 3....
Nom1 PNom1 Add1 DMA1 CL.M1 ccR1Nom2 PNom2 Add2 DMA2 CL.M2 ccR2Nom3 PNom3 Add3 DMA3 CL.M3 ccR3 ... Nomi PNomi Addi DMAi CL.Mi ccRi ...

I want the result as following :

Ligne 1....
Ligne 2....
Ligne 3....
"1","Nom1 ","PNom1 ","Add1 ","DMA1 ","CL.M1 ","ccR1"
"2","Nom2 ","PNom2 ","Add2 ","DMA2 ","CL.M2 ","ccR2"
"3","Nom3 ","PNom3 ","Add3 ","DMA3 ","CL.M3 ","ccR3"
...
"i","Nomi ","PNomi ","Addi ","DMAi ","CL.Mi ","ccRi
...

and this my cod
code:

Code: Select all

@echo on
mode 50,10&color a
set i=0
For /l %%a in (1,1,325) DO call :AAA %%a
z.txt
exit

:AAA
for /F "tokens=%1 delims=" %%a in (CLOLG.txt) DO (
echo "%i%","%%a">>z.txt
set /a i+=1
)
exit /B


THE PROBLEM's in ccRiNomi
Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Put columns on lines [Mettre des colonnes sur des lignes

#2 Post by foxidrive » 21 Jan 2014 08:27

You code does nothing except add a prefix and quotes.

If you provide an actual data file on dropbox then we can have a look.
The real data is needed - as in many cases it matters when creating the code, and fake data does not show all the relevant details.

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#3 Post by alid0381 » 21 Jan 2014 09:18

yes i knew that.. :shock:
But what is the solution :idea: :?: .

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#4 Post by alid0381 » 21 Jan 2014 09:51

Also know the length of the field
It's 185 character and space :
Nomi<------ 185 character and space ------>ccRi
I would like to use this :

Code: Select all

echo %1:~%m%,%n%%>>z.txt

but I have not prospered.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Put columns on lines [Mettre des colonnes sur des lignes

#5 Post by penpen » 21 Jan 2014 10:05

foxidrive wrote:The real data is needed - as in many cases it matters when creating the code, and fake data does not show all the relevant details.
If you don't serve some real data, alid0381, then for example:
Should one guess if and how it is possible at all, to seperate the two connected strings in {ccR1Nom2, ..., ccRi-1Nomi}.
In common it is NOT possible, so one needs the real data, to specify if this may be possible, and how this could be achieved.

The space between Nomi and ccRi doesn't matter here, or did you mean this:

Code: Select all

/------ 185 character and space ------\/ -- ...   -- \/------ 185 character and space ------\
Nom1 ............................. ccR1Nom2 ... ccRi-1Nomi ............................. ccRi
\----------------------------- i times 185 character and space -----------------------------/
And what do you mean with "185 character and space":
- 185 + unknown variable amount of space characters, or
- 185 + unknown fixed amount of space characters, or
- 185 + known fixed amount of space characters, or
- 185 characters (spaces included)

penpen

Edit: Added "If ... :" right below the quote.

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#6 Post by alid0381 » 21 Jan 2014 11:39

Yes penpen

Code: Select all

And what do you mean with "185 character and space":

- 185 characters (spaces included)
∑max= 185 characters (spaces included)
see :

Nomi : min One letter and max 15 letters.
PNomi : min One letter and max 15 letters.
Addi : min One letter and max 10 letters.
...
see
If Nomi contains 13 characters, So the rest 2 spaces :wink:

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Put columns on lines [Mettre des colonnes sur des lignes

#7 Post by penpen » 21 Jan 2014 12:19

So as it seems you don't want to serve real data:
Can you give a full description of it?
For example if only alphanumerics and spaces are allowed:

Code: Select all

<DataField> ::= ('a' | ... | 'z' | 'Z' | ... | 'Z' | '0' | ... | '9' | ' ')^185
Intention for this question: There are "pure batch poison" characters (for example ('!', '&', '|', ... ).

penpen

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#8 Post by alid0381 » 21 Jan 2014 12:34

Another explanation:

I wrote this code

Code: Select all

@echo on
mode 50,10&color a
set i=0
for /F "skip=3 tokens=1 delims=" %%a in (CLOLG.txt) DO call :BBB %%a
z.txt
exit

:BBB
set /a k=185*%i%+%i%
set /a l=185*(1+%i%)+%i%
echo %1:~%k%,%l%%>>z.txt
set /a i+=1
exit /B


the result :
%1:~0,185% <=> Nom1 PNom1 Add1 DMA1 CL.M1 ccR1
%1:~186,371% <=> Nom2 PNom2 Add2 DMA2 CL.M2 ccR2
...
%1:~k,l% <=> Nomi PNomi Addi DMAi CL.Mi ccRi

But there is a problem I did not understand

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#9 Post by alid0381 » 21 Jan 2014 13:02

penpen
See this <Database> :

....
TADJSE01 NNNN PPPPPPPP 19941001F3 630.00 0.00 630.0006/2009 0.001 01TADJSE011 NNNNNNNNN PPPPPP 19900401F3 630.00 0.00 630.0012/2001F.C 2000.00 0.00 01TADJSE012.....

TADJSE01,TADJMO10 : Addresse. (max 10 characters)
NNNN,NNNNNNNNN : Names. (max 15 characters)
PPPPPPPP,PPPPPP : prenames. (max 15 characters)
19941001F3 : 1994/10/01 date and F3 House Type. (max 10 characters)
...
F.C 2000.00 : observation. (max 80 characters)
...

Code: Select all

/------ 185 character and space ------\/ -- ...   -- \...
TADJ..........................0.001  01TADJ  ... .....ni...

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Put columns on lines [Mettre des colonnes sur des lignes

#10 Post by Squashman » 21 Jan 2014 13:39

Is the input data space delimited or is it a TRUE Fixed Length data file?
I do data processing on Fixed Length files. So for example if I had a line with the persons Name and Address the layout of that line would be as follows.
FULLNAME Position 1-50
ADDRESS Position 51-100
CITY Position 101-130
STATE Postion 131-132
ZIPCODE Position 133-142

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Put columns on lines [Mettre des colonnes sur des lignes

#11 Post by penpen » 21 Jan 2014 14:02

alid0381 wrote:But there is a problem I did not understand
I guess:
- the symptom is: The for loop does not produce any output.
- the cause is either: The line the for loop processes has more than 8090 characters
- or either the cause is: The last line in the text file does not end with a carriage return, newline
- the solution: use a "for /L" loop and read, as long as data is in the line, the next data block using:

Code: Select all

@echo off
setlocal enableDelayedExpansion
(
   (
      for /L %%a in (1,1,4) do (
         if %%a LSS 4 (
            call :skipLine
         ) else (
            call :processLine
         )
      )
   ) < "CLOLG.txt"
) > "z.txt"
endlocal
goto :eof


:skipLine
   set "input="
   set /P "input="
   set "lastByte=!input:~1022!"
   if defined lastByte goto :skipLine
   goto :eof

:processLine
   set "buffer= "
:readRestOfLine
   set "input="
   set /P "input="
   set "buffer=!buffer!!input!"
:outputNextDataField
   set "lastByte=!buffer:~185,1!"
   if defined lastByte (
      echo(!buffer:~1,185!
      set "buffer=!buffer:~0,1!!buffer:~186!"
      goto :outputNextDataField
   ) else if defined input (
      set "lastByte=!input:~1022!"
      if defined lastByte goto :readRestOfLine
   )
   set "buffer=!buffer:~1!"
   if defined buffer echo(invalid data: !buffer:~1!
   goto :eof
If this is the partition of datafields, you need, then just add the line numbers.

The above may fail if special key characters (TAB, ...) are included, or if some poison characters are present ('!', '&', '|', ...).

If i have guessed wrong, please test the above script, if it partitionates the data fields right, and if not describe the problem.

penpen

Edit: If there are additional lines, then you also must add detection of \n as a first character, as could be seen here:
http://www.dostips.com/forum/viewtopic.php?p=28851#p28851.
Last edited by penpen on 22 Jan 2014 04:53, edited 1 time in total.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Put columns on lines [Mettre des colonnes sur des lignes

#12 Post by Aacini » 21 Jan 2014 20:36

Wow! I hate to try to write code when the specifications are incomplete and we are forced to guess in order to produce any code that may run. @alid0381: you should realize that show program code trying to explain something never works: if the code is wrong, and we don't know what its purpose is, how we could identify any errors on it? You always should explain your problem in plain English and include both the real input data and the desired output.

Anyway, this is my attempt to try to hit on the solution to this problem:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem I have a text file "CLOLG.txt" that contains 4 Ligns :
rem Ligne 1....
rem Ligne 2....
rem Ligne 3....
rem Nom1 PNom1 Add1 DMA1 CL.M1 ccR1Nom2 PNom2 Add2 DMA2 CL.M2 ccR2Nom3 PNom3 Add3 DMA3 CL.M3 ccR3 ... Nomi PNomi Addi DMAi CL.Mi ccRi ...

rem I want the result as following :
rem Ligne 1....
rem Ligne 2....
rem Ligne 3....
rem "1","Nom1 ","PNom1 ","Add1 ","DMA1 ","CL.M1 ","ccR1"
rem "2","Nom2 ","PNom2 ","Add2 ","DMA2 ","CL.M2 ","ccR2"
rem "3","Nom3 ","PNom3 ","Add3 ","DMA3 ","CL.M3 ","ccR3"
rem ...
rem "i","Nomi ","PNomi ","Addi ","DMAi ","CL.Mi ","ccRi

set recLen=31

set /A line=0, i=0
for /F "delims=" %%a in (CLOLG.txt) do (
   set "input=%%a"
   set /A line+=1
   if !line! leq 3 (
      echo !input!
   ) else (
      call :ProcessLine4
   )
)
goto :EOF

:ProcessLine4
   set /A i+=1
   set output="%i%"
   set record=!input:~0,%recLen%!
   for %%b in (%record%) do set output=!output!,"%%b "
   echo %output:~0,-2%"
   set input=!input:~%recLen%!
if defined input goto ProcessLine4
exit /B


The "recLen" variable should be set to the record lenght, 185 in your case. This program may show a maximum of 44 sub-records of 185 characters each. The value given in the program is the right one for the example data:

Code: Select all

Ligne 1....
Ligne 2....
Ligne 3....
Nom1 PNom1 Add1 DMA1 CL.M1 ccR1Nom2 PNom2 Add2 DMA2 CL.M2 ccR2Nom3 PNom3 Add3 DMA3 CL.M3 ccR3Nomi PNomi Addi DMAi CL.Mi ccRi


Output:

Code: Select all

Ligne 1....
Ligne 2....
Ligne 3....
"1","Nom1 ","PNom1 ","Add1 ","DMA1 ","CL.M1 ","ccR1"
"2","Nom2 ","PNom2 ","Add2 ","DMA2 ","CL.M2 ","ccR2"
"3","Nom3 ","PNom3 ","Add3 ","DMA3 ","CL.M3 ","ccR3"
"4","Nomi ","PNomi ","Addi ","DMAi ","CL.Mi ","ccRi"


Antonio

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Put columns on lines [Mettre des colonnes sur des lignes

#13 Post by penpen » 22 Jan 2014 05:27

Some other problems in your script are within this line:

Code: Select all

echo %1:~%k%,%l%%>>z.txt
As the replacement extensions are only available for environment variables, and not for command line parameter variables: See "set /?".
So you have to store the command line parameter within a variable.
And you need to use delayedExpansion or a call construct to build the variable replacement command and then use it.

Code: Select all

:: needed for both possibilities
set param_1=%1

:: 1. possibility, also used by Aacini:
setlocal enableDelayedExpansion
echo !param_1:~%k%,%l%!>>z.txt
endlocal
:: 2.possibility
call echo %%param_1:~%k%,%l%%%>>z.txt


Note: You only have space for |_8092 /185 _| = 44 datasets per line if you want to parse them using a "for /F" loop.

penpen

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#14 Post by alid0381 » 22 Jan 2014 07:26

Thanks PenPen :D
Now, It's working..

The result is like this form :

Code: Select all

Nomi PNomi Addi DMAi CL.Mi ccRi


But, Can I make it like this form :

Code: Select all

"i","Nomi ","PNomi ","Addi ","DMAi ","CL.Mi ","ccRi"

... :? :?:

alid0381
Posts: 28
Joined: 09 May 2012 10:37

Re: Put columns on lines [Mettre des colonnes sur des lignes

#15 Post by alid0381 » 22 Jan 2014 09:14

I mean
Can I split : 185 to :

Code: Select all

echo  "i","%1:~0,15%","%1:~16,30%","%1:~31,k%","%1:~k+1,l%",....,"%1:~n,185%">>"z.txt"

:!: :?: :idea:
Last edited by alid0381 on 22 Jan 2014 09:38, edited 4 times in total.

Post Reply