0

I have a txt file in format:

text1.text2.text3   text4   text5   text6
text7.text8.text9   text10  text11  text12
etc....

I need a script which will make a new txt file wih format:

<record>
<string id="day_of_month" value="text1"/>
<string id="month" value="text2"/>
<string id="year" value="text3"/>
<string id="time" value="tekst4"/>
<string id="home_team_id" value="text5"/>
<string id="away_team_id" value="text6"/>
</record>
<record>
<string id="day_of_month" value="text7"/>
<string id="month" value="text8"/>
<string id="year" value="text9"/>
<string id="time" value="text10"/>
<string id="home_team_id" value="text11"/>
<string id="away_team_id" value="text12"/>
</record>
etc....

etc.... mean that the number of rows isn't defined.

  • 1
    Ok, soooo... have you tried coming up with one? We're not gonna write it for you. You can ask specific questions about the script but don't expect us to do your work for you. – Max Leske Feb 16 '14 at 13:59
  • I don't know almost anything about programing. I've only wrote a bat to copy files from folder to folder and back (backup and restore) and wrote 1 macro for excel in my life. Could you please give me some hint to functions which could be used in this sort of problem that I can google it? :) – user3316048 Feb 16 '14 at 14:12

2 Answers2

1

The following script will work:

@echo off

set in=in.txt
set out=output.txt

(for /f "tokens=1-6 delims=. " %%i in (
    %in%
) do (
    echo ^<record^>
    echo ^<string id="day_of_month" value="%%i"/^>
    echo ^<string id="month" value="%%j"/^>
    echo ^<string id="year" value="%%k"/^>
    echo ^<string id="time" value="%%l"/^>
    echo ^<string id="home_team_id" value="%%m"/^>
    echo ^<string id="away_team_id" value="%%n"/^>
    echo ^</record^>
)) > %out%
mbroshi
  • 969
  • 8
  • 21
  • When I start this bat from this: `12.07.2013 2100 432 440` I get this: ` ` same problem for each row – user3316048 Feb 16 '14 at 14:20
  • I think that the problem is because between day, month and year in original txt is dot, and between year, time, home id, and away id in original txt is tab – user3316048 Feb 16 '14 at 14:29
  • I get it. Just after .(dot) in the script I delete space and put tab. Thank you man – user3316048 Feb 16 '14 at 14:35
  • 1
    +1, Performance can be improved if the entire FOR loop is enclosed by parentheses and then after the closing paren redirect output using `>"%out%"`. Then you don't need to redirect each line of output using append mode, which saves time because each redirection in your code takes time to open the file and position the file pointer to the end of file. – dbenham Feb 16 '14 at 18:17
  • @dbenham how to enclosed FOR loop and how that change will look like? – user3316048 Feb 27 '14 at 10:07
  • @user3316048 I edited script with suggested improvement. – mbroshi Feb 27 '14 at 11:49
0

The very efficient solution below uses REPL.BAT - a hybrid JScript/batch utility that performs a regular expression search and replace on each line of stdin and writes the result to stdout. The utility is pure native script that will run on any modern Windows machine from XP onward, without any need of 3rd party .exe files. Full documentation is embedded within REPL.BAT.

Assuming REPL.BAT is in your current directory, or better yet, somewhere within you PATH, then:

@echo off
setlocal enableDelayedExpansion

:: Setup search and replace strings
set "search=(.*?)\.(.*?)\.([\S]*)\s*([\S]*)\s*([\S]*)\s*(.*)"
set "repl=<record>\r\n"
set "n=1"
for %%A in (day_of_month month year time home_team_id away_team_id) do (
  set "repl=!repl!<string id=\q%%A\q value=\q$!n!\q/>\r\n"
  set /a n+=1
)
set "repl=!repl!</record>"

:: Do all the work
type input.txt|repl.bat search repl xv >output.txt
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390