如何使用批处理脚本组合两个文本文件

时间:2022-02-10 00:12:34

How to remove string and replace with content found in text file using batch script.

如何使用批处理脚本删除字符串并替换为在文本文件中找到的内容。

test.txt

Version: 4.5.0
Import:
   //MPackages/Project/config/abc.txt                       #head
   //Packages/Project/config/cde.txt                        #head
View: 24234
  //MPackages/Project/config/ac.txt                     #head

Remove any text found between "Import:" and "View:" and replace it with content from sample text file..

删除“导入:”和“查看:”之间找到的任何文本,并将其替换为示例文本文件中的内容。

sample.txt

1
2
3

Desired output

Version: 4.5.0
Import:
   1
   2
   3
View: 24234
   //MPackages/Project/config/ac.txt                     #head

1 个解决方案

#1


You could do this with pure batch, but a robust solution is actually quite complicated. I've pretty much quit doing text processing with pure batch.

你可以用纯批量做到这一点,但一个强大的解决方案实际上非常复杂。我几乎放弃了使用纯批处理进行文本处理。

Instead, I use JREPL.BAT - a hybrid JScript/batch regular expression text processor that runs natively on any Windows machine from XP onward.

相反,我使用JREPL.BAT - 一个混合的JScript /批处理正则表达式文本处理器,可以在XP之后的任何Windows机器上本机运行。

The following one liner works fine both from the command line, and within a batch script. It assumes "Import:" and "View:" only appear once.

以下一个内衬可以从命令行和批处理脚本中正常工作。它假设“导入:”和“视图:”仅出现一次。

jrepl "^" "   " /f sample.txt | jrepl "^(Import:)[\s\S]*?(^View:)" "$1+'\r\n'+stdin.ReadAll()+$2" /j /m /f test.txt /o output.txt

#1


You could do this with pure batch, but a robust solution is actually quite complicated. I've pretty much quit doing text processing with pure batch.

你可以用纯批量做到这一点,但一个强大的解决方案实际上非常复杂。我几乎放弃了使用纯批处理进行文本处理。

Instead, I use JREPL.BAT - a hybrid JScript/batch regular expression text processor that runs natively on any Windows machine from XP onward.

相反,我使用JREPL.BAT - 一个混合的JScript /批处理正则表达式文本处理器,可以在XP之后的任何Windows机器上本机运行。

The following one liner works fine both from the command line, and within a batch script. It assumes "Import:" and "View:" only appear once.

以下一个内衬可以从命令行和批处理脚本中正常工作。它假设“导入:”和“视图:”仅出现一次。

jrepl "^" "   " /f sample.txt | jrepl "^(Import:)[\s\S]*?(^View:)" "$1+'\r\n'+stdin.ReadAll()+$2" /j /m /f test.txt /o output.txt