批处理脚本合并多行

时间:2019-01-10 18:27:11

标签: windows batch-file cmd

我有一个包含多行的文本文件:

Mario Rossi 1:17.36 +0.00   1.33    (2) 1.34    (6) 11.46   (2) 1.43    (1) 1.32    (1) 7.55    (1) 3.40    (1) 8.44    (1) 1.21    (19)    12.40   (24)
SWE         1.33    (2) 3.07    (2) 14.53   (2) 16.36   (1) 18.08   (1) 26.03   (1) 29.43   (1) 38.27   (1) 39.48   (1) 52.28   (1)
Alex Haas 1:18.11   +0.35   1.28    (1) 1.35    (10)    11.35   (1) 2.04    (34)    1.42    (6) 8.47    (7) 3.57    (4) 9.26    (2) 1.18    (5) 11.23   (2)
NOR         1.28    (1) 3.03    (1) 14.38   (1) 16.42   (2) 18.24   (2) 27.11   (2) 31.08   (2) 40.34   (2) 41.52   (2) 53.15   (2)

我想同时合并两行,以便输出看起来像:

Mario Rossi 1:17.36 +0.00   1.33    (2) 1.34    (6) 11.46   (2) 1.43    (1)
SWE                         1.33    (2) 3.07    (2) 14.53   (2) 16.36   (1)
1.32    (1) 7.55    (1) 3.40    (1) 8.44    (1) 1.21    (19)    12.40   (24)
18.08   (1) 26.03   (1) 29.43   (1) 38.27   (1) 39.48   (1)     52.28   (1)

Alex Haas 1:18.11   +0.35   1.28    (1) 1.35    (10)    11.35   (1) 2.04    (34)
NOR                         1.28    (1) 3.03    (1)     14.38   (1) 16.42   (2)
1.42    (6) 8.47    (7) 3.57    (4) 9.26    (2) 1.18    (5) 11.23   (2)
18.24   (2) 27.11   (2) 31.08   (2) 40.34   (2) 41.52   (2) 53.15   (2)

所有行对都具有相同的格式。这些行中可以有更多元素,因此我需要将它们合并到更多行中(例如,输出第一行的前12个元素,然后输出第二行的前12个元素,然后输出第一行的下12个元素,然后输出下12个第二行的元素,依此类推,直到到达该行的末尾为止。

到目前为止,我只能找到连接多行的解决方案,而不必混合行的内容(How to combine multiple lines in a single text file into one line, in Windows?Batch - Combine two lines and add comma to reformat txt to csv)。因此,我停留在下面的代码中,这些代码我不知道如何完成:

@echo off
set _inputfile=myfile
setlocal enabledelayedexpansion
set evenflag=1
for /f "tokens=*" %%x in (%_inputfile%.txt) do set x1=!x2! && set x2=%%x && (
set /a evenflag^^=1 && if !evenflag!==1 (
    rem how to process !x1! and !x2! to get desired output?
))
pause

2 个答案:

答案 0 :(得分:1)

正如Gerhard Barnard所建议的,用于此目的的批处理脚本将变得非常复杂。我可以使用以下Perl脚本轻松解决问题:

#!/usr/bin/perl
my $elementtoprint = 12; #how many elements per line are printed in the output
local $" = "\t"; #set tab as default separator when printing an array

open my $info, "input.txt" or die "Could not open $file: $!";

while ( my $line1 = <$info> )
{
    defined(my $line2 = <$info>) or last;
    #convert strings into arrays
    my @tab1 = split(/\s+/, $line1);
    my @tab2 = split(/\s+/, $line2);

    #handle first output lines (with people's names) differently
    my @subtab1 = splice (@tab1,0,$elementtoprint);
    my @subtab2 = shift(@tab2);
    my @subtab3 = splice (@tab2,0,$elementtoprint-4);
    print "@subtab1 \n";
    print "@subtab2 \t\t\t\t@subtab3 \n";

    while(@tab1&&@tab2)
    {
        my @subtab1 = splice(@tab1,0,$elementtoprint);
        my @subtab2 = splice(@tab2,0,$elementtoprint);
        print "@subtab1 \n";
        print "@subtab2 \n";
    }
    print "\n";
}

close $info;

答案 1 :(得分:0)

此代码可以执行您想要的操作:

@echo off
setlocal EnableDelayedExpansion

set "spaces28=                            "
set "line1="
for /F "delims=" %%a in (input.txt) do (
   if not defined line1 (
      set "line1=%%a"
   ) else (
      set "line2=%%a"
      call :SplitLines
   )
)
goto :EOF


:SplitLines

set "header=1"
:nextPart

   for /F "tokens=12*" %%a in ("%line1%") do (
      if "%%b" equ "" (set "head=%line1%") else set "head=!line1:%%b=!"
      set "line1=%%b"
   )
   set /P "=%head%" < nul
   echo/

   if %header% equ 1 (
      for /F "tokens=9*" %%a in ("%line2%") do (
         if "%%b" equ "" (set "head=%line2%") else set "head=!line2:%%b=!"
         for /F "tokens=1*" %%c in ("!head!") do (
            set "head=%%c%spaces28%"
            set "head=!head:~0,28!%%d"
         )
         set "line2=%%b"
      )
      set "header=0"
   ) else (
      for /F "tokens=12*" %%a in ("%line2%") do (
         if "%%b" equ "" (set "head=%line2%") else set "head=!line2:%%b=!"
         set "line2=%%b"
      )
   )
   set /P "=%head%" < nul
   echo/

if defined line1 goto nextPart
echo/

exit /B

输出示例:

Mario Rossi 1:17.36 +0.00   1.33    (2) 1.34    (6) 11.46   (2) 1.43    (1) 
SWE                         1.33    (2) 3.07    (2) 14.53   (2) 16.36   (1) 
1.32    (1) 7.55    (1) 3.40    (1) 8.44    (1) 1.21    (19)    12.40   (24)
18.08   (1) 26.03   (1) 29.43   (1) 38.27   (1) 39.48   (1) 52.28   (1)

Alex Haas 1:18.11   +0.35   1.28    (1) 1.35    (10)    11.35   (1) 2.04    (34)    
NOR                         1.28    (1) 3.03    (1) 14.38   (1) 16.42   (2) 
1.42    (6) 8.47    (7) 3.57    (4) 9.26    (2) 1.18    (5) 11.23   (2)
18.24   (2) 27.11   (2) 31.08   (2) 40.34   (2) 41.52   (2) 53.15   (2)