-1

How to merge the first N words of multiple tmp files using cmd. I tried merging the first line but when I check the text file, it is showing 4 lines for every 1 line in the tmp files

EXAMPLE:

TMP FILE 1: FILE NAME: GALILEO1 CONTENT:

SAMPLE WORDS TO GET THE FIRST EIGHT WORD1 FOR EACH FILE
SAMPLE WORDS TO GET THE FIRST EIGHT WORDS FOR EACH FILE
SAMPLE WORDS TO GET THE FIRST EIGHT WORDS FOR EACH FILE

TMP FILE 2: FILE NAME: GALILEO2 CONTENT:

SAMPLE WORDS TO GET THE FIRST EIGHT WORD2 FOR EACH FILE
SAMPLE WORDS TO GET THE FIRST EIGHT WORDS FOR EACH FILE
SAMPLE WORDS TO GET THE FIRST EIGHT WORDS FOR EACH FILE

DESIRED OUTPUT IN TXT:

GALILEO1.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD1
GALILEO2.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD2

The tmp files contains multiple words in one line but when I extracted it using below code, the text file shows it as 4 lines.

@echo off
setlocal EnableDelayedExpansion
if exist galileo.txt del galileo.txt
for %%f in (*.tmp) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
    )

    echo %%f, Galileo !line1! >> galileo.txt
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    Please explain the kind of "merge" you are seeking. Could you provide two (2) short .tmp files and the result of merging them? – lit Sep 19 '18 at 14:47
  • Sure, let me attach samples in the post. Merge meaning multiple tmp files then the first let us say 8 words for each is what I need to be combined into one text files. Thanks! – K.Gulseven Sep 20 '18 at 06:22

2 Answers2

0

Presuming the first N Words on the first line, not overall:

  • Read the first line with Set /p and redirection into a variable
  • use a for /f to parse first 8 (space/tab delimited) words.

:: Q:\Test\2018\09\20\SO_52396744.cmd
@echo off
setlocal EnableDelayedExpansion
if exist galileo.txt del galileo.txt
set i=0
( for %%f in (*.tmp) do (
      set /A i+=1
      Set /P "line="<%%f
      for /F "tokens=1-8" %%A in ('echo:!line!') do (
        echo Galileo!i!.TMP %%A %%B %%C %%D %%E %%F %%G %%H
      )
  )
) > galileo.txt

sample output:

> type galileo.txt
Galileo1.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD1
Galileo2.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD2
  • Suggestion_ use `%%f` or `%%~nxf` instead of `Galileo!i!.TMP` to get the real filenames (you don't know, if the files are continuous numbered and start with `1`) – Stephan Sep 20 '18 at 17:10
  • Thank you both! I forgot to mention that I need to put the word "Galileo" together with the file name (file names will be anything). Reason why combining your suggestion and my previous code, I came up with below and it worked: @echo off setlocal EnableDelayedExpansion if exist galileo.txt del galileo.txt set i=0 ( for %%f in (*.tmp) do ( set /A i+=1 Set /P "line="<%%f for /F "tokens=1-7" %%A in ('echo:!line!') do ( echo %%f, Galileo %%A %%B %%C %%D %%E %%F %%G ) ) ) > galileo.txt – K.Gulseven Sep 21 '18 at 09:36
0

If the files are not large, the following PowerShell script can be used in a .bat file script. This will interweave the first eight (8) words of each line into a new file.

Yes. I know you did not ask for a PowerShell solution. It is another option on any current-day Windows system.

powershell -NoProfile -Command ^
    "$output_file = '.\galileo.txt';" ^
    "" ^
    "$f1 = Get-Content -Path '.\galileo1.txt';" ^
    "$f2 = Get-Content -Path '.\galileo2.txt';" ^
    "" ^
    "if (Test-Path -Path $output_file) { Remove-Item -Path $output_file };" ^
    "" ^
    "$maxline = [Math]::max($f1.Length, $f2.Length);" ^
    "" ^
    "for ($i=0; $i -lt 5; $i++) {" ^
    "    if ($f1[$i]) {" ^
    "        $a = $f1[$i].Split();" ^
    "        $s = $a[0..7] -join ' ';" ^
    "        Out-File -InputObject $s -FilePath $output_file -Append -Encoding ascii" ^
    "    };" ^
    "    if ($f2[$i]) {" ^
    "        $a = $f2[$i].Split();" ^
    "        $s = $a[0..7] -join ' ';" ^
    "        Out-File -InputObject $s -FilePath $output_file -Append -Encoding ascii" ^
    "    };" ^
    "}" ^

If you put this code into a file (such as merge.ps1), it can be invoked from a .bat file script by:

powershell -NoProfile -File .\merge.ps1

This would be the contents of merge.ps1.

$output_file = '.\galileo.txt'

$f1 = Get-Content -Path .\galileo1.txt
$f2 = Get-Content -Path .\galileo2.txt

if (Test-Path -Path $output_file) { Remove-Item -Path $output_file }

$maxline = [Math]::max($f1.Length, $f2.Length)

for ($i=0; $i -lt 5; $i++) {
    if ($f1[$i]) {
        $a = $f1[$i].Split()
        $s = $a[0..7] -join ' '
        Out-File -InputObject $s -FilePath $output_file -Append -Encoding ascii
    }
    if ($f2[$i]) {
        $a = $f2[$i].Split()
        $s = $a[0..7] -join ' '
        Out-File -InputObject $s -FilePath $output_file -Append -Encoding ascii
    }
}
lit
  • 14,456
  • 10
  • 65
  • 119
  • Hi! Unfortunately, most of the PCs that will use the script don't have Powershell. In any case, thank you so much for your help! :) – K.Gulseven Sep 21 '18 at 09:50