使用cmd/批处理文件编辑txt文件

问题描述 投票:0回答:1

我有一个如下所示的txt文件:

Name,Easting,Northing,Height,Epoch
1,2666650.148,1259312.319,412.011,2024.02.12 10:30:3541
2,2666650.148,1259312.318,412.011,2024.02.12 10:31:1210

我需要将其重新排列为如下所示:

DateTime;SensorName;CustomerName;Flags;SensorType;Unit;East;North;Height
2024.02.12 10:30;1;1;;Prism;Meter;2666650.148;1259312.319;412.011
2024.02.12 10:30;2;2;;Prism;Meter:2666650.148;1259312.318;412.011
  • 标头是固定的,可以更换。
  • 目前,我只需要一次,无论是哪一次,并且没有秒。
  • 两个名称相同
    Name
    Flags
    不存在,
    Sensortype
    Unit
    已修复。

我设法做了一些,但无法正确地将它们组合在一起。我对此的理解只是来自尝试和错误。我可以使用 VBA 脚本,但我不想使用 Excel。周日半夜往往会无缘无故卡住。

batch-file cmd
1个回答
0
投票

好的。这是我给您的建议,关于 “按照您的要求使用 y 来执行 x”

您可以按照seb在她的评论中建议的方式通过批处理文件解决此问题。但是,您也可以使用我的 printf.exe 版本 2.11 程序以非常不同的方式解决此问题。

我的 printf.exe 应用程序是一个 Windows 控制台程序,它是众所周知的 printf CRT 函数的包装器,从而允许文本和格式化数值显示在 cmd.exe 窗口中。此外,我的程序 printf.exe 还允许使用基于堆栈的 Hewlett-Packard 的相同方法和功能对 32 位整数和 64 位 double 浮点数执行 Reverse Polish Notation 算术运算计算器.

新的 printf.exe 版本 2.11 还管理字符串操作,并允许使用最简单的编程方案编写脚本(程序)。下面的批处理文件包含一个 printf.exe 程序,可以解决这个问题。

@echo off

< input.txt printf "DateTime;SensorName;CustomerName;Flags;SensorType;Unit;East;North;Height\n"  ^
   OUT          /* Show output header                   */ ^
   80 IN        /* Read input header and discard it     */ ^
   FMT{ "%%s;%%s;%%s;;Prism;Meter;%%s;%%s;%%s\n"        /* Output format */ ^
   (            /* WHILE read line                      */ ^
      80        /*    80  (max line length)             */ ^
      IN?       /*    line_read len                     */ ^
      ^<        /*    Drop len and get:                 */ ^
                /*    "Name,Easting,Northing,Height,Epoch"      */ ^
      ","       /*    line delims                               */ ^
      split1    /*    "Name" "Easting" "Northing" "Height" "Epoch" 5        Split 5 substrings      */ ^
      ^<        /*    "Name" "Easting" "Northing" "Height" "Epoch"          Drop the 5              */ ^
      0 -5 gets /*    "Name" "Easting" "Northing" "Height" "Epoch" "Epo"    Cut seconds from Epoch  */ ^
      ^<2       /*    "Name" "Easting" "Northing" "Height" "Epo"            Drop original Epoch     */ ^
      }5        /*    "Easting" "Northing" "Height" "Epo" "Name"            Roll Up Name            */ ^
      ^>        /*    "Easting" "Northing" "Height" "Epo" "Name" "Name"     Duplicate it            */ ^
      {6 {6     /*    "Name" "Name" "Easting" "Northing" "Height" "Epo"     Roll Down both Names    */ ^
      {6        /*    "Epo" "Name" "Name" "Easting" "Northing" "Height"     Roll Down Epoch         */ ^
      OUT       /*    Output result     */ ^
      ^<*       /*    Clear all data    */ ^
   :            /* REPEAT               */ ^
   )            /* ENDWHILE             */   > output.txt

输出:

DateTime;SensorName;CustomerName;Flags;SensorType;Unit;East;North;Height
2024.02.12 10:30;1;1;;Prism;Meter;2666650.148;1259312.319;412.011
2024.02.12 10:31;2;2;;Prism;Meter;2666650.148;1259312.318;412.011

虽然看起来很复杂,但 printf.exe 指令非常简单。 HP计算器用户在了解差异和编程方案后,可以在几分钟内开始编写printf.exe程序。您可以在 Base64 解码器

中查看 printf.exe 程序的更大示例

您可以从此链接

下载printf.exe包
© www.soinside.com 2019 - 2024. All rights reserved.