Batch: merge two files but in a specific order

1

Let me start this by mentioning that I am new into this programming in batch files. That said, I would like to merge two files, and I started with the following code:

copy Test1.sol + Test2.sol Test.sol /B

This code simply puts the content of Test2.sol at the bottom of Test1.sol, which is not what I want to do. Instead I would like to put the content of Test2.sol in a specific place in Test1.sol.

To make it more clear. Let's say that the content of Test2.sol is:

401 TEXT1  12345       123444
401 TEXT2  12345       123444
401 TEXT3  12345       123444

And the content of Test1.sol is:

**********************
**     HEADER 1     **
**********************

Message1
Message2

**********************
**     HEADER 2     **
**********************

**********************
**     HEADER 3     **
**********************

Message3
Message4

How can I put the content of Test1.sol under HEADER 2 in Test2.sol - like the other messages?

Thanks in beforehand!

EMS

Posted 2016-12-28T12:50:55.383

Reputation: 13

I don't think batch files can do this. VBScript can do it though – LPChip – 2016-12-28T13:02:19.707

Dang it. Maybe you can help me with another thing then. Do I have to create a new thread for that purpose - or can I ask you here? – EMS – 2016-12-28T13:05:19.650

if its not related, create a new post. – LPChip – 2016-12-28T13:07:06.133

@LPChip That could be done with a batch file but it would be a one-off solution and very complicated - I'm not prepared to spend that much time writing it :) – DavidPostill – 2016-12-28T13:21:03.323

Unfortunately, I can only create a new thread every 40 minutes. As this is a bit urgent, is it possible that I can send a private message from this homepage? – EMS – 2016-12-28T13:31:29.657

Answers

0

Your best bet is to break the files up.

If you don't want to break it up you need a lot of echos like this

@echo off
@echo ********************** >results.txt
@echo **     HEADER 1     ** >>results.txt
@echo ********************** >>results.txt
type value1.txt >>results.txt

h1.txt

**********************
**     HEADER 1     **
**********************

Message1

Message2

h2.txt

**********************
**     HEADER 2     **
**********************

h3.txt

**********************
**     HEADER 3     **
**********************

Message3

Message4

type h1.txt >results.txt
type value1.txt >>results.txt
type h2.txt >results.txt
type value2.txt >>results.txt
type h3.txt >results.txt
type value3.txt >>results.txt

Obviously you can append as many sections as you want. The only trick left is separating the original list into individual files.

It isn't clear what goes under each header, but I will show an example.

find "401 " values.txt >values1.txt
find "402 " values.txt >values2.txt
find "403 " values.txt >values3.txt

That could work.

cybernard

Posted 2016-12-28T12:50:55.383

Reputation: 11 200

That actually worked with just three divisions! Thank you a lot! – EMS – 2016-12-28T14:54:52.717

1

There is a "simple" one-liner solution with JREPL.BAT - a regular expression command line find/replace text processing utility. JREPL.BAT is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required. Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged help.

jrepl "^" "stdin.readAll()+'\r\n'" /j /inc "/^\*\*     HEADER 2     \*\*$//+3" /f test1.sol /o test.sol <test2.sol

Explanation of how it works, working backwards (sort of):

  • /f test1.sol Specifies the main input file.
  • /o test.sol Specifies the output file to be created.
  • <test2.sol Provides the 2nd file as input on stdin.
  • /inc "/...//+3" Specifies that the find/replace is only to occur on the 3rd line after the
    first "Header 2" line. The text between the forward slashes is a regular expression.
  • /j Specifies that the replace argument is to be interpreted as JScript.
  • "^" The find argument is a regular expression that matches the beginning of a line.
  • "stdin..." The replace argument is a JScript expression that evaluates to the entire content of stdin (the 2nd file), plus an extra line terminator.

dbenham

Posted 2016-12-28T12:50:55.383

Reputation: 9 212