tomasz86

How to merge two text files?

238 posts in this topic

There are two files:

1.txt

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568

2.txt

[SourceFileInfo]
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

If I do "copy 1.txt+2.txt 3.txt" I get this:

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
[SourceFileInfo]
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

but I would like to get something similar to this:

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

Is it possible?

0

Share this post


Link to post
Share on other sites

sort 3.txt /o 4.txt

One liner (but needs fixing by someone more knoledgable):

copy 1.txt+2.txt | sort /o 3.txt

GL

0

Share this post


Link to post
Share on other sites
Beyond Compare is the way to go... it's not free, but it's worth the cost.
0

Share this post


Link to post
Share on other sites

Or using unix tools:

grep -vi "\[SourceFileInfo\]" 1.txt  >>2.txt

"\" are need to escape the "[" used with grep for regular expression.

0

Share this post


Link to post
Share on other sites

Thank you very much :) I'll check them for sure. I'm especially interested in this unix one as it could be used in a batch script.

By the way, could anyone help me with this script?

MD HFMER TEMP2
DIR/B/A-D/OGN/ON HF\*.EXE>HF.TXT
SET HF=
FOR /F %%I IN (HF.TXT) DO (SET HF=%%I&IF DEFINED HF CALL :HFEXTRACT)
DEL/Q/S HF.TXT

REM ======================TYPE 1 HOTFIXES=================================================
:HFEXTRACT
TITLE %T1% - Processing %HF%
ECHO %HF%
MD TEMP&START/WAIT HF\%HF% /Q /X:TEMP
XCOPY/DEHRY TEMP HFMER
MOVE TEMP\UPDATE\update.inf HFMER\UPDATE\%HF%.inf
MOVE TEMP\UPDATE\update_w2k.inf HFMER\UPDATE\%HF%.inf
MOVE TEMP\UPDATE\update.ver TEMP2\%HF%.ver
DEL/Q/S HFMER\UPDATE\update*.inf HFMER\UPDATE\update.ver
RD/Q/S TEMP
CLS
REM =====================================================================================

COPY/B TEMP2\*.ver HFMER\UPDATE\update.ver
SORT HFMER\UPDATE\update.ver /O HFMER\UPDATE\update.ver
RD/Q/S TEMP2

This is a mix of strings taken from HFSLIP and my own but the problem is that I need the last three lines to go after the rest of the script is finished. It work correctly if I remove the last line but the folder TEMP2 is not deleted then.

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

I've managed to overcome it:

IF EXIST HF\*.EXE (
MD HFMER TEMP2
DIR/B/A-D/OGN/ON HF\*.EXE>HF.TXT
SET HF=
FOR /F %%I IN (HF.TXT) DO (SET HF=%%I&IF DEFINED HF CALL :HFEXTRACT)
DEL/Q/S HF.TXT
CALL :HFCS
)
REM ======================TYPE 1 HOTFIXES=================================================
:HFEXTRACT
TITLE %T1% - Processing %HF%
ECHO %HF%
MD TEMP&START/WAIT HF\%HF% /Q /X:TEMP
XCOPY/DEHRY TEMP HFMER
MOVE TEMP\UPDATE\update.inf HFMER\UPDATE\%HF%.inf
MOVE TEMP\UPDATE\update_w2k.inf HFMER\UPDATE\%HF%.inf
MOVE TEMP\UPDATE\update.ver TEMP2\%HF%.ver
DEL/Q/S HFMER\UPDATE\update*.inf HFMER\UPDATE\update.ver
RD/Q/S TEMP
IF NOT EXIST HF.TXT (
COPY/B TEMP2\*.ver HFMER\UPDATE\update.ver
SORT HFMER\UPDATE\update.ver /O HFMER\UPDATE\update.ver
RD/Q/S TEMP2
)
REM ======================================================================================

I have a question about this Unix tool called grep.

When I have only two files:

1.ver

[SourceFileInfo]
basesrv.dll=7F87C84D34813197A2360CEA800A7464,0005000008931B27,46352
cmd.exe=7705AED861C7FDBD919E771A1B42B5AA,0005000008931AA8,236304

2.ver

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760

after "grep -vi "\[sourceFileInfo\]" 1.ver >>2.ver"

[SourceFileInfo]
clusapi.dll=B95AC82B54FE4359C3453264F848509A,0005000008931AA8,55568
clusnet.sys=A0610690266ED57A2D04EA5D7EC8084C,0005000008931AA8,67760
basesrv.dll=7F87C84D34813197A2360CEA800A7464,0005000008931B27,46352
cmd.exe=7705AED861C7FDBD919E771A1B42B5AA,0005000008931AA8,236304

Everything is OK. What about more than two files? Is it possible to use grep with multiple files?

Also an another question (2 questions ;)): Can the list be sorted alphabetically? Is there a way to remove duplicates?

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

Yes grep can work on multiple files and no it can't sort or remove duplicates unless you do the scripting for it .

0

Share this post


Link to post
Share on other sites

How should I use this command

grep -vi "\[SourceFileInfo\]" 1.txt  >>2.txt

when there are multiple files?

I tried something like this:

grep -vi "\[SourceFileInfo\]" *.txt  >>3.txt

but it doesn't work. Also tried doing

copy 1.txt+2.txt 3.txt
grep -vi "\[SourceFileInfo\]" 3.txt >>3.txt

but after doing this there is no longer [sourceFileInfo] in the final file.

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

grep -vih "\[SourceFileInfo\]" 1.txt 2.txt >>3.txt

But then if you need sorting and finding duplicates to choose the most recent version of each file, you might want to use vbs or autoit as it would be a lot more easier to script.

Edited by allen2
0

Share this post


Link to post
Share on other sites

I see. Any way to do it for a larger number of files automatically, ex. 50 text files in one directory?

0

Share this post


Link to post
Share on other sites

Yes something like this should work (i added the sort part with the unix tool "sort.exe") :

set filenames=
for /f "delims= usebackq" %%i in (`dir /b %target%\*.inf`) do (set %filenames%=%filenames% %%i)
echo [SourceFileInfo] >result.txt
grep -vih "\[SourceFileInfo\]" %filenames% |sort -d >>result.txt

Due to batch variables limitations %filenames% can't have more than 2047 characters so it might not work with long filenames.

Edited by allen2
0

Share this post


Link to post
Share on other sites

It's getting a little bit complicated for me as I'm just a beginner when it comes to scripting ;)

Anyway, I'll try to get the above script to work.

0

Share this post


Link to post
Share on other sites

@allen2

You don't actually *need* grep or any other "third party" program for such a simple task.

You can use either FIND /V "[" to exclude the line(s) containing square brackets or or use a FOR /F with a SKIP=1 (if that line is always the first one), or use FIND or FINDSTR to only get lines containing commas.

This could do:

@ECHO OFF
IF EXIST result.txt DEL result.txt
FOR /F %%A IN ('dir /b *.txt') DO (
FOR /F "skip=1 tokens=*" %%B IN (%%A) DO (
ECHO %%A -- %%B
ECHO %%B >>lines.txt
)
)
ECHO [SourceFileInfo] >result.txt
SORT lines.txt >>result.txt
DEL lines.txt
ECHO.
MORE result.txt

jaclaz

0

Share this post


Link to post
Share on other sites

@jaclaz

I hate the dos find because i always had hard time getting it to do what i wanted ; and also, as i began scripting on unix , i always find first an algorithm translation with a batch adapted from some kind of unix shell scripting.

Anyway, i never said my script coding was the best or even good.

That said, i would do what tomasz86 is trying to do (extracting hotfix, getting files with the higher version in one .inf to create a service pack) with autoit (for the duplicate file part) but a batch script might be easy for you.

0

Share this post


Link to post
Share on other sites

Thank you very much :thumbup

I changed it like this:

@ECHO OFF
FOR /F %%A IN ('dir /b *.ver') DO (
FOR /F "skip=1 tokens=*" %%B IN (%%A) DO (
ECHO %%A -- %%B
ECHO %%B >>TEMP2\update.txt
)
)
ECHO [SourceFileInfo] >HFMER\UPDATE\update.ver
SORT TEMP2\update.txt >>HFMER\UPDATE\update.ver
DEL TEMP2\update.txt
ECHO.
MORE HFMER\UPDATE\update.ver

but I can't set the folder for '*.ver' files to be 'TEMP2\*.ver'. How should I edit this line?

FOR /F %%A IN ('dir /b *.ver') DO (

Edited by tomasz86
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.