Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 更改txt文件的批处理脚本_Batch File - Fatal编程技术网

Batch file 更改txt文件的批处理脚本

Batch file 更改txt文件的批处理脚本,batch-file,Batch File,我有一个txt文件,格式如下: text1.text2.text3 text4 text5 text6 text7.text8.text9 text10 text11 text12 etc.... 我需要一个脚本,它将生成一个新的txt文件,格式如下: <record> <string id="day_of_month" value="text1"/> <string id="month" value="text2"/> <stri

我有一个txt文件,格式如下:

text1.text2.text3   text4   text5   text6
text7.text8.text9   text10  text11  text12
etc....
我需要一个脚本,它将生成一个新的txt文件,格式如下:

<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....

等等。。。。表示未定义行数。

以下脚本将起作用:

@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%
@echo关闭
set in=in.txt
set out=output.txt
(对于/f“代币=1-6个代币=。”%i英寸)(
%以%计
)做(
回音^
回音^
回音^
回音^
回音^
回音^
回音^
回音^
))>%out%

下面的非常有效的解决方案使用的是对stdin的每一行执行正则表达式搜索和替换,并将结果写入stdout。该实用程序是纯本机脚本,从XP开始将在任何现代Windows计算机上运行,无需任何第三方.exe文件。完整的文档嵌入在REPL.BAT中

假设REPL.BAT在您当前的目录中,或者更好,在您的路径中的某个地方,那么:

@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
@echo关闭
setlocal enableDelayedExpansion
::设置搜索和替换字符串
设置“搜索=(.*?)\(.*?)([\S]*)\S*([\S]*)\S*([\S]*)\S*(.*)”
设置“repl=\r\n”
设置“n=1”
对于%%A in(日、月、月、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年、年(
设置“repl=!repl!\r\n”
设置/a n+=1
)
设置“repl=!repl!”
::做所有的工作
键入input.txt | repl.bat search repl xv>output.txt

好的,很好。。。你试过想出一个吗?我们不会为你写的。你可以问一些关于脚本的具体问题,但不要指望我们能帮你完成工作。我对编程几乎一无所知。我只写过一个bat来将文件从一个文件夹复制到另一个文件夹(备份和恢复),并在我的一生中为excel编写了一个宏。你能不能给我一些提示,让我用谷歌搜索一下这类问题中可能用到的函数当我从下面开始这个bat:
12.07.2013 2100 432 440
我得到了:
每个行都有相同的问题,我认为问题是因为原始txt中的日、月和年之间是点,而原始txt中的年、时、家庭id和外出id之间是tabI明白了。(点)之后,我在脚本中删除空格并放置制表符。感谢man+1,如果整个FOR循环用括号括起来,然后在结束paren后使用
>“%out%”重定向输出,则性能可以得到提高。这样,您就不需要使用追加模式重定向每一行输出,这节省了时间,因为代码中的每个重定向都需要时间来打开文件并将文件指针定位到文件的末尾。@dbenham如何将FOR循环括起来,以及该更改的外观如何?