0

我有一个文本文件,其中的列使用竖线字符(“|”)分隔。csv 有数百列,我只想保留前 254 列。如何创建一个批处理脚本,该脚本将创建一个新的 output.txt 文件,其中仅包含我请求的列?

4

2 回答 2

1
@echo off
setlocal EnableDelayedExpansion

(for /F "delims=" %%a in (input.csv) do (
   set "input=%%a"
   set "output="
   for /L %%b in (1,1,254) do (
      for /F "tokens=1* delims=|" %%c in ("!input!") do (
         set "output=!output!|%%c"
         set "input=%%d"
      )
   )
   echo !output:~1!
)) > output.txt
于 2013-11-14T01:09:24.307 回答
0

还有一个批处理/jscript 混合脚本解决方案。如果另存为split.cmd, 调用

type inputfile.txt | split.cmd "|" 254 > outputfile.txt

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
    setlocal enableextensions

    rem arguments : delimiter numberOfColumns

    set "delimiter=%~1"
    if not defined delimiter set "delimiter=,"

    set "columns=%~2"
    if not defined columns set "columns=10000"

    cscript //nologo //e:Javascript "%~f0" 

    endlocal
    exit /b

@end
// **** Javascript zone *****************************************************
    var IN  = WScript.StdIn, 
        OUT = WScript.StdOut;
    var shell       = WScript.CreateObject("WScript.Shell"),
        delimiter   = shell.Environment("PROCESS").Item("delimiter"), 
        columns     = shell.Environment("PROCESS").Item("columns") ; 

    while ( ! IN.AtEndOfStream ){
        OUT.WriteLine(IN.ReadLine().split(delimiter).slice(0,columns).join(delimiter));
    };
于 2013-11-14T09:13:51.120 回答