Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Script to transform

I need a DOS batch program that will transform the content of a file as per below requirements:

Input file: ASM.txt
ORIGIN=
SEASON=
SKDFILE=
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
ASM
UTC
18OCT00009E206
RPL
MH6144/20OCT11 3/MH 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

1. Find the keyword RPL then change ASM at the header(above line) to SSM
SSM                                             (ASM->SSM)
UTC
18OCT00009E206
RPL
MH6144/20OCT11 3/MH 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

2. Then go to the next line after RPL, find the first '/', move any words between '/' and a space to the next new line and then remove the first '/'
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH 6/MH6145/21
20OCT11                        --------------> new line
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

3. Replicate the words that have been moved to the new line and separate both words with a space
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH 6/MH6145/21
20OCT11 20OCT11                  --------------> new line
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

4. Maintain 3/MH then move any words after 3/MH to end of the new line and separate with space
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH
20OCT11 20OCT11 6/MH6145/21      --------------> new line
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

Output file: SSM.txt
ORIGIN=
SEASON=
SKDFILE=
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH
20OCT11 20OCT11 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

Let me know if you have any question.

Thanks.
0
Rizuan
Asked:
Rizuan
  • 17
  • 9
1 Solution
 
paultomasiCommented:
I've made some assumptions here so please check and report back... Seenotes below.
@echo off
setlocal enabledelayedexpansion

set flag=
del ssm.txt asm.tmp 2>nul

for /f "tokens=*" %%a in (asm.txt) do (
   if not defined flag (
      if /i "%%a"=="RPL" set flag=1

      if /i "%%a"=="ASM" (
         echo SSM>>ssm.txt
      ) else (
         echo %%a>>ssm.txt
      )
   ) else (
      echo %%a>>asm.tmp
   )
)

for /f "tokens=1-3* delims= " %%a in (asm.tmp) do (
   set str=%%~a	
   echo !str:~0,6! %%~b>>ssm.txt
   echo !str:~7,7! !str:~7,7! %%~c>>ssm.txt
   goto :end-for
)
:end-for

more +1 <asm.tmp >>ssm.txt
del asm.tmp

Open in new window

I have assumed that in the line:

   MH6144/20OCT11 3/MH 6/MH6145/21

The first part (MH6144) is always 6 characters wide.
The second part (20OCT11) is always 7 characters wide.

If this is not the case let me know and I will approach it dynamically...
0
 
RizuanAuthor Commented:
Hi paultomasi

So far it replaces ASM to SSM and here is the result
ORIGIN=PC_AIRFLITE
SEASON=W03
SKDFILE=c:\pcaf_ncms\summer_crg\summer_data\utcsched.skd[235,236]
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL
MH6144/20OCT11 3/MH 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250


As for the first part, it either 5 or 6 characters wide and the second part is either 6 or 7 characters wide.
0
 
paultomasiCommented:
okay... i expected a slight hitch at this point... give me a mo.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
paultomasiCommented:
Please try this updated version...
@echo off
setlocal enabledelayedexpansion

set flag=
del ssm.txt asm.tmp str.tmp 2>nul

for /f "tokens=*" %%a in (asm.txt) do (
   if not defined flag (
      if /i "%%a"=="RPL" set flag=1

      if /i "%%a"=="ASM" (
         echo SSM>>ssm.txt
      ) else (
         echo %%a>>ssm.txt
      )
   ) else (
      echo %%a>>asm.tmp
   )
)

for /f "tokens=1-3* delims= " %%a in (asm.tmp) do (
   echo %%a>str.tmp
   for /f "tokens=1,2 delims=/" %%A in (str.tmp) do (
      echo %%A %%b>>ssm.txt
      echo %%B %%B %%c>>ssm.txt
   )
   goto :end-for
)
:end-for

more +1 <asm.tmp >>ssm.txt
del asm.tmp str.tmp 2>nul

Open in new window

0
 
paultomasiCommented:
Here is the result I got from your sample file in the question... As you can see, all appears to be well.

ORIGIN=
SEASON=
SKDFILE=
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH
20OCT11 20OCT11 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250
0
 
RizuanAuthor Commented:
I tried with the actual file and it did not work at first until i removed an extra space character after the word RPL.

ORIGIN=
SEASON=
SKDFILE=
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL -----------------------------------------------------> one extra space character
MH6144/20OCT11 3/MH 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

Can you do something about it. Attached is the sample of the actual file.
asm.txt
0
 
RizuanAuthor Commented:
paultomasi,
I'm so sorry actually there is another additional requirement and it might a little bit challenge for you.

asm.txt:
ORIGIN=PC_AIRFLITE
SEASON=W03
SKDFILE=c:\pcaf_ncms\summer_crg\summer_data\utcsched.skd[235,236]
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL
MH6144/20OCT11 3/MH 6/MH6145/21
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250

After transforming the content, need to check the day of the date (e,g 20OCT11) .
Monday = 1
Tuesday = 2
Wedesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
And since 20OCT11 is on Thursday
Then the output should be like this:

ssm.txt
ORIGIN=PC_AIRFLITE
SEASON=W03
SKDFILE=c:\pcaf_ncms\summer_crg\summer_data\utcsched.skd[235,236]
DATE=111018
TIME=113910
TRANSASM=00001
TRANSSSM=00003
SSM
UTC
18OCT00009E206
RPL
MH6144 3/MH
20OCT11 20OCT11 4 6/MH6145/21 ------------------------> refer to this line.
F 74K VVMH74K
KUL2115 SHJ210415
SHJ210620 FRA211250


0
 
paultomasiCommented:
Apologies... Just returned home.

Will address your query within the next hour.
0
 
paultomasiCommented:
With regard to your comment http:#36987521.

I am not able to produce a space character after 'RPL' given the sample data from your question and my code (http:#36985329). I find it very peculiar that this is happening to you.

I am running this in XP Pro. I copy & pasted the contents of ASM.TXT straight into Notepad and saved it as ASM.TXT.

After running my batch file, the resultant SSM.TXT contains no extra spaces.

What operating system are you running this under?

Can you please try the code again (copy it from above and recreate the batch file) then, aslo copy the ASM.TXT text from above and recreate the ASM.TXT file. Please report back the resultant SSM.TXT file.

0
 
RizuanAuthor Commented:
I'm running on Windows XP and i also believe this only happen on my local. Never mind i will handle the issue with the extra space.
0
 
paultomasiCommented:
Rizuan

I'm concerned about the extra space you mentioned.

The reply "I will handle the issue with the extra space" does not enable me to determine WHY you get an extra space - why it works flawlessly for me, but not for you.

It doesn't confirm wether it's a problem with your source file (ASM.TXT), the batch file (I doubt this very much having examined the code more closely) or some other strange computing / system anomoly.

My interest is (as a programmer) finding a work-around if it's something out of the ordinary.

0
 
paultomasiCommented:
With regard to the day-of-week thing, yes it is very do-able and hope to have a working solution in a mo....
0
 
paultomasiCommented:
Here is the additional DOW code requested.
@echo off
setlocal enabledelayedexpansion

set flag=
del ssm.txt asm.tmp str.tmp 2>nul

for /f "tokens=*" %%a in (asm.txt) do (
  if not defined flag (
    if /i "%%a"=="RPL" set flag=1

    if /i "%%a"=="ASM" (
      echo SSM>>ssm.txt
    ) else (
      echo %%a>>ssm.txt
    )
  ) else (
    echo %%a>>asm.tmp
  )
)

for /f "tokens=1-3* delims= " %%a in (asm.tmp) do (
  echo %%a>str.tmp
  for /f "tokens=1,2 delims=/" %%A in (str.tmp) do (
    call :GetDOW %%B dow
    echo %%A %%b>>ssm.txt
    echo %%B %%B !dow! %%c>>ssm.txt
  )
  goto :end-for
)
:end-for

more +1 <asm.tmp >>ssm.txt
del asm.tmp str.tmp 2>nul
exit /b


:GetDOW
  set month=janfebmaraprmayjunjulaugsepoctnovdec
  set str=%1

  set d=%str:~0,-5%
  set y=%str:~-2%

  for /l %%i in (0,1,11) do (
    set /a i=%%i*3
    for %%j in (!i!) do if /i "!month:~%%j,3!"=="!str:~-5,3!" set /a m=%%i +1
  )

  set /a a=(14-m)/12
  set /a y=y-a
  set /a m=m+12*a-2
	
  set /a %2=(d+y+y/4-y/100+y/400+(31*m)/12)%%7
goto :eof

Open in new window

0
 
RizuanAuthor Commented:
Actually the extra space after the word RPL was produced by the source system. This only happen if the keyword is RPL. I assume it is part of the system design.

Anyway i have edited the code to handle this issue. At line 9:
    if /i "%%a"=="RPL" set flag=1
i have added one extra space after the word RPL: and it works.
    if /i "%%a"=="RPL " set flag=1

I have tested and i'm very happy with the result but can you improve the code so that it can handle if there is more than one RPL.
0
 
paultomasiCommented:
So, are you saying there might be more than one occurance of RPL in a single ASM.TXT file?

If that's the case, will each occurance of RPL be followed by the same formatted line as we've been working on? AND, will THEY also need to be processed too?

What about the ASMs?
0
 
paultomasiCommented:
Regarding the RPL issue...

The batch file will output "RPL " (including the space character) to SSM.TXT if that's what it finds in ASM.TXT. Similarly, if it finds just "RPL" then it will output just "RPL".

If what you want in the SSM.TXT file is just "RPL" regardless of what it finds in ASM.TXT then I can make that adjustment.

How about just testing to see if the first 3 characters are 'RPL' then outputting 'RPL' if they are? This relies on the fact that no other line will contain the letters 'RPL' at the start of the line except RPL-lines themselves... Is this likely to be the case?
0
 
RizuanAuthor Commented:
Yes, in a single file, it can occur more than RPL. Each RPL is followed with the same formatted line and need to be processed too.

There will only be one ASM generated daily and I'm planning to trigger this batch job once a day.

As for RPL issue, i can assure there is no other line will contain RPL at the start of the line except for the RPL-lines themselves.

0
 
RizuanAuthor Commented:
Hi paultomasi,
Sorry to bother you, is there any update on the solution.
0
 
paultomasiCommented:
Sure... I post an update shortly
0
 
paultomasiCommented:
Apologies... got pulled away.

I am actually sitting here right now though and will post back in a moment...
0
 
paultomasiCommented:
Many apologies for the delay. Please accept the following....
@echo off
setlocal enabledelayedexpansion

set "flag="
del ssm.txt asm.tmp str.tmp 2>nul

for /f "tokens=*" %%a in (asm.txt) do (
  set line=%%a
  if /i "!line!"=="ASM" (
    echo SSM>>ssm.txt
  ) else (
    if /i "!line:~0,3!"=="RPL" (
      set flag=1
      echo RPL>>ssm.txt
    ) else (
      if defined flag (
        echo !line!>asm.tmp

        for /f "tokens=1-3* delims= " %%b in (asm.tmp) do (
          echo %%b>str.tmp

          for /f "tokens=1,2 delims=/" %%A in (str.tmp) do (
            call :GetDOW %%B dow
            echo %%A %%c>>ssm.txt
            echo %%B %%B !dow! %%d>>ssm.txt
          )
        )

        set "flag="
      ) else (
        echo !line!>>ssm.txt
) ) ) )

del asm.tmp str.tmp 2>nul
exit /b


:GetDOW
  set month=janfebmaraprmayjunjulaugsepoctnovdec
  set str=%1

  set d=%str:~0,-5%
  set y=%str:~-2%

  for /l %%i in (0,1,11) do (
    set /a i=%%i*3
    for %%j in (!i!) do if /i "!month:~%%j,3!"=="!str:~-5,3!" set /a m=%%i +1
  )

  set /a a=(14-m)/12
  set /a y=y-a
  set /a m=m+12*a-2
	
  set /a dd=(d+y+y/4-y/100+y/400+(31*m)/12)%%7
  if %dd% equ 0 (set %2=7) else (set %2=%dd%)  
goto :eof

Open in new window

0
 
paultomasiCommented:
Oh, I forgot to mention...

I have taken care of spaces after RPL in ASM.TXT, and ensured only RPL (without spaces) get written sent to SSM.TXT.

This happens in line 12 of the code where the first 3 characters of the line are compared rather than the whole line itself.
0
 
RizuanAuthor Commented:
Sorry for the late response. I have tested and it works perfectly. Thanks for your great solution.
If i encounter any issue with the script will you be able to look into it when i post again the question related to this.
0
 
paultomasiCommented:
Yes... of course!
0
 
RizuanAuthor Commented:
So far the best expert that i have ever liaise with. Very commited and know the right question to ask.

Hopefully i be able to deal with paultomasi again in the future.
0
 
paultomasiCommented:
Thank you for that tremendous praise.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 17
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now