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

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

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

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:

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

#17 Post by Aacini » 22 Jan 2014 10:04

alid0381 wrote: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"

... :? :?:


That is the output of my solution. Didn´t saw you it?

Aacini wrote: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

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

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

#18 Post by alid0381 » 22 Jan 2014 10:18

What if the Nomi touches PNomi
Means:
Nomi is in the max (max 15 characters, in our example)
and PNomi is in the max (max 15 characters)

Output, Would certainly like this :

Code: Select all

...
"i","NomiPNomi ","Addi ","DMAi ","CL.Mi ","ccRi"
...
:wink: :|

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

#19 Post by Aacini » 22 Jan 2014 17:17

I think I know what the problem is. You should realize that the space is a character and that each field have a fixed number of characters. For example, if Nomi field have 15 characters, it always have 15 characters not matter if some (or all, or none) of these characters are spaces! This way, a field have not "a max number of characters" (it always have the same) and any field always "touch" the next one, even if this "touch" is via a space!

You had not said us what is the number of characters in each field (just Nomi: 15, PNomi: 15, and Addi: 10), so you must complete the sizes of the other 3 fields in the code below. The sum of the six numbers must be equal to 185.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Define the lenghts of all fields
set /A numFields=0, recLen=0
for %%a in (15 15 10 lenOfDMAi lenOfCL.Mi lenOfccRi) do (
   set /A numFields+=1
   set /A len[!numFields!]=%%a, recLen+=%%a
)

set /A line=0, i=0, start=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 /L %%j in (1,1,%numFields%) do (
      for /F "tokens=1,2" %%m in ("!start! !len[%%j]!") do set output=!output!,"!record:~%%m,%%n!"
      set /A start+=len[%%j]
   )
   echo %output%
   set input=!input:~%recLen%!
if defined input goto ProcessLine4
exit /B


Antonio

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

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

#20 Post by penpen » 22 Jan 2014 18:53

Beside:
alid0381 wrote: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:
Well it is just analog to, my above post:
penpen wrote: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.
The %1 is a command line parameter and no environment variable:
You cannot use extended variable replacement on it.

penpen

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

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

#21 Post by foxidrive » 22 Jan 2014 23:29

This is where it leads to without a good sample of the actual data. Frustration and wasted time and effort.
The OPs can mask confidential data by changing some characters, without changing the length of each word.

IMHO there are some cases where the question should be ignored if they fail to provide a decent sample of the data.

It's hard enough trying to figure out when data is TAB delimited to start with :)

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

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

#22 Post by Squashman » 23 Jan 2014 07:30

I still can't figure out what the user is trying to do.

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

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

#23 Post by foxidrive » 23 Jan 2014 08:29

That makes two of us.

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

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

#24 Post by penpen » 23 Jan 2014 13:13

I think the task should (seems to :) ) be clear: Convert headered database1 to headered database2.
Database 1:
- loctation: line 4 of CLOLG.txt.
- format: list of datasets, 185 chars each

Database:
- location: line 4 and following z.txt
- format: comma separated values encapsulated within doublequotes, one dataset each line

header unchanged.

And there are two solutions for that:
1. Aacinis above, in two versions (using "for /F" as the OP seems to prefer to use; with a maximum of 44 datasets):
- the first version with dataset length 31 (just the number must replaced by 185 to work, if i see it right: this length was unclear at that time)
- the second version serves the entry max length: 15 15 10 lenOfDMAi lenOfCL.Mi lenOfccRi
just need to relace the lenOfDMAi lenOfCL.Mi lenOfccRi with their real values and the source should work (if i see it right).

2. My own above, that has no maximum of datasets, as it is read using "set /P".
But because of that it has to deal with the problems of using "set/P" (function keys, \n as first char;see above).
This version doesn't enclose the values in doublequotes, and are seperated by spaces, but i don't think, it is that problem to add them to the code,
as alid0381 has done it (wrongly: using parameters instead of environment variables; but how to solve that is described above (with source code)).
(I haven't created one source, as he wants a "for /f" loop anyway.)

So if alid0381 has no further questions, i see the problem as solved (i hope i don't misinterpret anything :) ).

penpen

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

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

#25 Post by alid0381 » 30 Jan 2014 11:02

I miss you experts ... :)

Thanks penpen and Aacini
You understand me
Excellent explanation.. :wink:

Aacini, I liked your script :

Code: Select all

...
for %%a in (15 15 10 lenOfDMAi lenOfCL.Mi lenOfccRi) do (
...

Were replaced by numbers.. :|

Code: Select all

...
for %%a in (10 15 15 8 4 7 9 10 7 80 5 2 3 10) do (
...


But Only the first three rows will be displayed.. :?:

Post Reply