使用 SED / VBS 将 Header 行插入 CSV
Insert Header Row into CSV using SED / VBS
首先 post 并为我是一个完全的新手而提前道歉。我继承了一些我只是想破解一个解决方案的东西,我对它的知识为零。我搜索了论坛并相信找到了 part-answer(sed 命令)但是,我遇到了一个问题,无法成功将其设置为 运行。
我需要将它 运行 放在 Windows 框上,它以前用于 .csv 文件中的简单 replace,我现在需要 插入 header 行 。
我有一个 'fixit.cmd' 文件,其中包含这个;
set batdir=C:\Sed\filepath\batch
set impdir=C:\Sed\filepath\import
set filename=xxx
:: to read as parameter, uncomment next line
:: set filename=%1
cscript //NoLogo %batdir%\sed.vbs 1i"ABC,123" < %impdir%\%filename%.csv > %impdir%\%filename%_fixed.csv
pause
我有一个 'sed.vbs' 文件,其中包含这个;
Dim pat, patparts, rxp, inp
pat = WScript.Arguments(0)
patparts = Split(pat,"/")
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
rxp.Pattern = patparts(1)
Do While Not WScript.StdIn.AtEndOfStream
inp = WScript.StdIn.ReadLine()
WScript.Echo rxp.Replace(inp, patparts(2))
Loop
当我 运行 'fixit.cmd' 我收到错误;
sed.vbs(7, 1) Microsoft VBScript runtime error: Subscript out of range: '[number: 1]'
我假设指向 'sed.vbs' 的内容仅支持之前的 replace 和/或我的 header 行插入字符串不正确。
我需要在 'sed.vbs' 内容和/或我的 header 行插入字符串中进行哪些修改才能成功 insert header 行?
非常感谢任何/所有支持。
像这样更改您的批处理文件:
set "batdir=C:\Sed\filepath\batch"
set "impdir=C:\Sed\filepath\import"
set "filename=xxx"
REM to read as parameter, uncomment next line
REM set filename=%1
>%temp%\header.txt echo ABC,123
copy /b "%temp%\header.txt" + "%impdir%\%filename%.csv" "%impdir%\%filename%_fixed.csv"
pause
不再需要 VBS 文件。
关于我所做更改的一些说明:
- 使用
set
命令的首选语法(防止杂散空格或某些特殊字符
- 评论命令为
REM
。 ::
是一个格式错误的标签(在大多数情况下有效,但在某些情况下会咬你。
- 路径引用(首选语法以避免文件夹名称或文件名中的空格或某些特殊字符出错)
第 >%temp%\header.txt echo ABC,123
行创建了一个包含 header 行的文件。
copy
命令连接两个文件(header 和您的文件),@luciole75w 已在评论中写道。
vbs文件在这里没有用,你可以放弃它。您的 fixit.cmd
文件可能如下所示:
@echo off
rem environment variables set after setlocal will be discarded on exit instead
rem of possibly altering the parent process (optional but good practice)
setlocal
set header_path="C:\Sed\filepath\batch\header.txt"
rem ~ removes quotes if any, so that input_path is always quoted no matter if
rem the argument is quoted or not (optional, easier to deal with spaces in paths)
set input_path="%~1"
rem optional checking
if %input_path%=="" echo missing input file path & exit /b 1
rem dpn = (d)rive + (p)ath + (n)ame, i.e. full path of input file without extension
set output_path="%~dpn1_fixed.csv"
rem concatenate header + input to output
copy /b %header_path% + %input_path% %output_path% >nul
此批处理文件以输入 csv 路径作为参数(绝对或相对路径,包括扩展名)调用。现在,如果您更喜欢即时生成 header,则可以将最后一行 (copy...
) 替换为 :
rem column names including special characters (&, |, < or >) must be quoted
rem the special character % must be doubled
set header="A|B=>C",50%%
rem write the header to the output (overwrite the file if it already exists)
rem parentheses are one way to avoid trailing spaces when redirecting
(echo %header%) > %output_path%
rem concatenate input to output
type %input_path% >> %output_path%
首先 post 并为我是一个完全的新手而提前道歉。我继承了一些我只是想破解一个解决方案的东西,我对它的知识为零。我搜索了论坛并相信找到了 part-answer(sed 命令)但是,我遇到了一个问题,无法成功将其设置为 运行。
我需要将它 运行 放在 Windows 框上,它以前用于 .csv 文件中的简单 replace,我现在需要 插入 header 行 。
我有一个 'fixit.cmd' 文件,其中包含这个;
set batdir=C:\Sed\filepath\batch
set impdir=C:\Sed\filepath\import
set filename=xxx
:: to read as parameter, uncomment next line
:: set filename=%1
cscript //NoLogo %batdir%\sed.vbs 1i"ABC,123" < %impdir%\%filename%.csv > %impdir%\%filename%_fixed.csv
pause
我有一个 'sed.vbs' 文件,其中包含这个;
Dim pat, patparts, rxp, inp
pat = WScript.Arguments(0)
patparts = Split(pat,"/")
Set rxp = new RegExp
rxp.Global = True
rxp.Multiline = False
rxp.Pattern = patparts(1)
Do While Not WScript.StdIn.AtEndOfStream
inp = WScript.StdIn.ReadLine()
WScript.Echo rxp.Replace(inp, patparts(2))
Loop
当我 运行 'fixit.cmd' 我收到错误;
sed.vbs(7, 1) Microsoft VBScript runtime error: Subscript out of range: '[number: 1]'
我假设指向 'sed.vbs' 的内容仅支持之前的 replace 和/或我的 header 行插入字符串不正确。
我需要在 'sed.vbs' 内容和/或我的 header 行插入字符串中进行哪些修改才能成功 insert header 行?
非常感谢任何/所有支持。
像这样更改您的批处理文件:
set "batdir=C:\Sed\filepath\batch"
set "impdir=C:\Sed\filepath\import"
set "filename=xxx"
REM to read as parameter, uncomment next line
REM set filename=%1
>%temp%\header.txt echo ABC,123
copy /b "%temp%\header.txt" + "%impdir%\%filename%.csv" "%impdir%\%filename%_fixed.csv"
pause
不再需要 VBS 文件。
关于我所做更改的一些说明:
- 使用
set
命令的首选语法(防止杂散空格或某些特殊字符 - 评论命令为
REM
。::
是一个格式错误的标签(在大多数情况下有效,但在某些情况下会咬你。 - 路径引用(首选语法以避免文件夹名称或文件名中的空格或某些特殊字符出错)
第 >%temp%\header.txt echo ABC,123
行创建了一个包含 header 行的文件。
copy
命令连接两个文件(header 和您的文件),@luciole75w 已在评论中写道。
vbs文件在这里没有用,你可以放弃它。您的 fixit.cmd
文件可能如下所示:
@echo off
rem environment variables set after setlocal will be discarded on exit instead
rem of possibly altering the parent process (optional but good practice)
setlocal
set header_path="C:\Sed\filepath\batch\header.txt"
rem ~ removes quotes if any, so that input_path is always quoted no matter if
rem the argument is quoted or not (optional, easier to deal with spaces in paths)
set input_path="%~1"
rem optional checking
if %input_path%=="" echo missing input file path & exit /b 1
rem dpn = (d)rive + (p)ath + (n)ame, i.e. full path of input file without extension
set output_path="%~dpn1_fixed.csv"
rem concatenate header + input to output
copy /b %header_path% + %input_path% %output_path% >nul
此批处理文件以输入 csv 路径作为参数(绝对或相对路径,包括扩展名)调用。现在,如果您更喜欢即时生成 header,则可以将最后一行 (copy...
) 替换为 :
rem column names including special characters (&, |, < or >) must be quoted
rem the special character % must be doubled
set header="A|B=>C",50%%
rem write the header to the output (overwrite the file if it already exists)
rem parentheses are one way to avoid trailing spaces when redirecting
(echo %header%) > %output_path%
rem concatenate input to output
type %input_path% >> %output_path%