Page 1 of 3

Converting one record to many

Posted: 17 Oct 2012 05:59
by RMSoares
Hi,
I’m receiving a file multi-format (in the same file can exist records with different formats), in the file the first record will be the header (the 1st character will have the letter "H"), then an record with the costumer identification (the 1st character of registration with the letter "C"), registration relating to movements (in the 1st character of registration with the letter "X" followed by a registration with the letter "R") and finally a record identifying the end of file (the 1st character registration with the letter "T").

Here's an example

H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY
T2012091111080000DFRFGT07609530000002030

In the case of the R-type registers, identifying the movements, the same record can have more than one movement (being the number of movements identified in column 14 to 16, "R20120911110403" being in this case referring to 03 movements) .

For these cases intended to turn that line into as many moves as the number of movements contained. for Example

R20120911110403 (1º movement) ALFA123456789CT (2º movement) BETA135798642CT (3º movement) ZETA246897531CT (can have more movents) Y (the last character refers the type of movements)

Intended to be transformed into
R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY


It should be noted that the last character refers to the type of movement (in the first record R-type is “X” an in the 2º R-Type the 3 movements have “Y” type) and is repeated for each movement processed.

The file should be convert to
H201209110000011022E
C10001 000R000000000040000000006
X0003012100000000000102200000784L
R20120911110101ALFA123456789CTX
X0003012100000110222010209000784L
R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY
T2012091111080000DFRFGT07609530000002030

Anyone have any idea how I can make this transformation?

Re: Converting one record to many

Posted: 17 Oct 2012 06:03
by RMSoares
Basically i whant to convert

R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY

in

R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY

Where the first 16 character is commom to all record and we must creted as many records as there are blocs of 15 characters. putting the last character at the end of each new record created

Re: Converting one record to many

Posted: 17 Oct 2012 06:21
by foxidrive
RMSoares wrote:Basically i whant to convert

R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY

in

R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY

Where the first 16 character is commom to all record and we must creted as many records as there are blocs of 15 characters. putting the last character at the end of each new record created


So the R type record has a defined structure of 16 characters, multiple blocks of 15 characters and a single descriptor as the last character.
How many 15 character blocks can there be in one R type record?

Can you use SED in a solution?

Re: Converting one record to many

Posted: 17 Oct 2012 06:37
by RMSoares
Can you send me an example of the used of SED ?

Re: Converting one record to many

Posted: 17 Oct 2012 07:07
by foxidrive
There was more than one question in my post. I can't give you a solution without the answer... and SED is a third party free program.

Re: Converting one record to many

Posted: 17 Oct 2012 07:50
by RMSoares
foxidrive wrote:How many 15 character blocks can there be in one R type record?

the number of blocks is variable, there may be records with 1 occurrences and in other cases with n occurrences.


foxidrive wrote:There was more than one question in my post. I can't give you a solution without the answer... and SED is a third party free program.

It will not be possible to use a third party free program

Re: Converting one record to many

Posted: 17 Oct 2012 07:54
by foxidrive
RMSoares wrote:
foxidrive wrote:How many 15 character blocks can there be in one R type record?

the number of blocks is variable, there may be records with 1 occurrences and in other cases with n occurrences.


And what is the range of n? 3 million?

Re: Converting one record to many

Posted: 17 Oct 2012 07:57
by abc0502
So any line that start with "R" and end with "Y" will be converted "sliced" based on the steps you mentioned.
and then re-create the file based on the new shape
is that it?

edit:
will be any empty lines in the files or it won't make a diffidence if it was there and then removed?

Re: Converting one record to many

Posted: 17 Oct 2012 08:05
by foxidrive
abc0502 wrote:So any line that start with "R" and end with "Y" will be converted "sliced" based on the steps you mentioned.
and then re-create the file based on the new shape
is that it?


No. As I understand it the last character can change. It has to be split up into 16 leading characters plus each subsequent 15 character block, and add the trailing character to the end of each line.

I want to know how many movements there can be because batch has line length limitations. If it's a large number of movements then VBS needs to be used.

Re: Converting one record to many

Posted: 17 Oct 2012 08:07
by RMSoares
abc0502 wrote:So any line that start with "R" and end with "Y" will be converted ....


No, not true, the last caracter could have any different letter (it defined the type of movement)

The Layout of record R is

R<14 charcater's><1 or more blocks with 15 character eache><1 character that will defined the type of movement>

R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY


abc0502 wrote:will be any empty lines in the files or it won't make a diffidence if it was there and then removed?

No, there not will have any empty lines

Re: Converting one record to many

Posted: 17 Oct 2012 08:08
by RMSoares
foxidrive wrote:
abc0502 wrote:I want to know how many movements there can be because batch has line length limitations. If it's a large number of movements then VBS needs to be used.


The number of movement is variable

Re: Converting one record to many

Posted: 17 Oct 2012 08:15
by abc0502
then any lines start with "R" and is 31 character long will be skipped but it it is more that 31 character long, that mean it will need to be modified and the "Y" will be the end of each 31 characters
or do you mean that the "Y" can be any other character and will be changed?

Re: Converting one record to many

Posted: 17 Oct 2012 08:23
by foxidrive
RMSoares wrote:
foxidrive wrote:
abc0502 wrote:I want to know how many movements there can be because batch has line length limitations. If it's a large number of movements then VBS needs to be used.


The number of movement is variable


You want FREE help, but you won't answer a question about the task.

Or is your English so poor that you can't understand what I've asked three times?

Re: Converting one record to many

Posted: 17 Oct 2012 09:15
by RMSoares
foxidrive wrote:You want FREE help, but you won't answer a question about the task.

Or is your English so poor that you can't understand what I've asked three times?


foxidrive, I appreciate any help you can give me, for I am not **consored** in VBS.

I am sorry that my English is bad, but try to answer all questions


The number of movements is variable, not being defined which the maximum number of movements that can appear in each record.
What is the maximum number of characters that can appear in each record?



For all entries beginning with "R" must be created as many blocks (of 15 characters) -1

As each record constituted by the first 15 characters, a block of 15 carcateres and last character.


Records with less than 31 characters does not need to be converted, the records of more than 31 characters will be converted.
The last character is variable can assume any value

following same examples
#example 1
R20120911110403ALFA123456789CTBETA135798642CTZETA246897531CTY

must be convert to:
R20120911110403ALFA123456789CTY
R20120911110403BETA135798642CTY
R20120911110403ZETA246897531CTY

#example 2
R20120912411802QWER123798642CTALFA544897531CTX

must be convert to:
R20120912411802QWER123798642CTX
R20120912411802ALFA544897531CTX

#example 3
R20120913210403BETA123006789CTXPTO135734642CTZETA566897531CTT

must be convert to:
R20120913210403BETA123006789CTT
R20120913210403XPTO135734642CTT
R20120913210403ZETA566897531CTT

#example 4
R20120914122404BETA123876329CTXPTO130545642CTQWER125648642CTZETA560891531CT

must be convert to:
R20120914122404BETA123876329CTT
R20120914122404XPTO130545642CTT
R20120914122404QWER125648642CTT
R20120914122404ZETA560891531CTT

#example 5
R20120915410401XPTO100421244CTS

-> does not need to be converted

Re: Converting one record to many

Posted: 17 Oct 2012 10:05
by abc0502
Test This:

Code: Select all

@Echo OFF
set "input=test.txt"
set "output=final.txt"

Setlocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in ('Type "%input%"') Do (
   set "line=%%A"
   IF "!line:~0,1!" NEQ "R" ( Echo !line!>>"%output%"
   ) Else (
      IF "!line:~0,1!" EQU "R" (
         Call :strLen "!line!" "len_line"
         IF "!len_line!" GTR "31" (
            set /a end = len_line - 15
            for /l %%b in (15,15,!end!) Do Echo !line:~0,15!!line:~%%b,15!!line:~-1!>>"%output%"
            ) Else ( Echo !line!>>"%output%" )
      )
   )

)
pause
Exit /B

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(
SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A%~1"
rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12,-1,0) do (
    set /a "len|=1<<%%A"
    for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
(
ENDLOCAL
REM RETURN VALUES
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

It check each line in the Input file,
If not start with "R" it put it in the output file, else it will check it's length,
if the length not greater than 31 it will put it in the output file, else:
get the end point which is the length of the line - the 15 constant characters, then in a for loop that conut from 15 to the end point and add 15 "letters that change" each time and combine the 1st 15 characters + the middle 15 characters + last char "whatever it is" 8)