【问题标题】:How to merge the first N words of multiple tmp files using cmd如何使用cmd合并多个tmp文件的前N个单词
【发布时间】:2018-09-19 01:32:20
【问题描述】:

如何使用cmd合并多个tmp文件的前N个单词。我尝试合并第一行,但是当我检查文本文件时,它在 tmp 文件中每 1 行显示 4 行

示例:

TMP 文件 1: 文件名:伽利略1 内容:

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 文件 2: 文件名:伽利略2 内容:

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

TXT 中的期望输出:

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

tmp 文件在一行中包含多个单词,但是当我使用以下代码提取它时,文本文件显示为 4 行。

@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
)

【问题讨论】:

  • 请解释您正在寻求的“合并”类型。您能否提供两 (2) 个短 .tmp 文件以及合并它们的结果?
  • 当然,让我在帖子中附上示例。合并意味着多个 tmp 文件,然后第一个让我们说每个 8 个单词是我需要合并到一个文本文件中的内容。谢谢!

标签: batch-file cmd merge word tmp


【解决方案1】:

假设 first 行的前 N ​​个单词,而不是整体:

  • Set /p 读取第一行并重定向到一个变量中
  • 使用 for /f 解析前 8 个(空格/制表符分隔)单词。

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

样本输出:

> type galileo.txt
Galileo1.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD1
Galileo2.TMP SAMPLE WORDS TO GET THE FIRST EIGHT WORD2

【讨论】:

  • 建议_使用%%f%%~nxf而不是Galileo!i!.TMP来获取真实的文件名(你不知道,如果文件连续编号并以1开头)
  • 谢谢你们!我忘了提到我需要把“伽利略”这个词和文件名放在一起(文件名可以是任何东西)。将您的建议和我以前的代码结合起来的原因,我想出了下面并且它起作用了:@echo off setlocal EnableDelayedExpansion if exists galileo.txt del galileo.txt set i=0 ( for %%f in (*.tmp) do ( set /A i+=1 Set /P "line=" galileo.txt
【解决方案2】:

如果文件不大,可以在 .bat 文件脚本中使用以下 PowerShell 脚本。这会将每行的前八 (8) 个单词交织到一个新文件中。

是的。我知道您没有要求 PowerShell 解决方案。这是任何当前 Windows 系统上的另一种选择。

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" ^
    "    };" ^
    "}" ^

如果您将此代码放入文件(例如 merge.ps1),则可以通过以下方式从 .bat 文件脚本调用它:

powershell -NoProfile -File .\merge.ps1

这将是 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
    }
}

【讨论】:

  • 嗨!不幸的是,大多数将使用该脚本的 PC 都没有 Powershell。无论如何,非常感谢您的帮助! :)
猜你喜欢
  • 2017-09-12
  • 2013-03-14
  • 2018-10-20
  • 1970-01-01
  • 2018-08-16
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
相关资源
最近更新 更多