通用文本转换器 [英] Generic Text Converter

查看:41
本文介绍了通用文本转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个通用批处理脚本,该脚本将读取一个模式文件,该文件将包含固定宽度平面文件源的各种宽度/列长度,并最终根据列长度创建目标csv文件.

I want to make a generic batch script which will read a schema file which will contain the various width's/column length's of the fixed width flat file source and finally create a target csv file based on the column length.

示例:

Schema.txt

Schema.txt

COL1,5
COL2,2
COL3,4
COL4,3
COL5,6

因此上面的schema.txt文件包含列列表,还包含每个字段的宽度.我们的源代码始终是固定宽度的平面文件.我们的目标是将其转换为csv.

So the above schema.txt file contains the column list.It also contains the width of each field. Our source will always be a fixed width flat file. Our objective will be to convert it into csv.

Source1.txt

Source1.txt

11111223333444555555
11111223333444555555

Target1.txt

Target1.txt

11111,22,3333,444,555555
11111,22,3333,444,555555

Source2.txt

Source2.txt

11111  333344466666
11111223333   66666

Target2.txt

Target2.txt

11111,,3333,444,66666
11111,22,333,,66666

所以它也应该能够处理空格和空格,正如我们在第二个源文件中所看到的.模式应该是一个动态文件,如果我们提供结构,bat文件将创建一个与源代码中的结构完全相同的csv.最终目标文件应具有从模式文件中获取的标头.请帮忙.

so it should be able to handle space and blanks as well, as we saw in 2nd Source file. The schema should be a dynamic file where if we provide the structure the bat file will create a csv exactly like the structure from the source.The final target file should have the header taken from the schema file. Please help.

我现在的代码如下:

echo off
setlocal EnableDelayedExpansion
echo a,b,c    final.txt
rem replace the €€€ string with any unused one
set "fooString=€€€" 
for /f "tokens=1 delims=;" %%i in (source.txt) do (
  set "x=%%i"
  for /f "tokens=1,2 delims=," %%a in (config.txt) do (
    call SET "VARraw=!x:~%%a,%%b!%fooString%"
    rem replaced with respect to the OP's comment: for %%p in (!VARraw!) do (
    for /F "tokens=*" %%p in ("!VARraw!") do (
      set "rav=%%p"
      set "var=!rav:%fooString%=!"
      echo/|set /p "=!var!,"
    )         final.txt
  )
)

当前config.txt包含

Present config.txt contains

0,9
9,3
12,11
23,7
30,1

但是我想修改它.只想保留字段名称和宽度.不是起始位置和宽度.

But i want to modify it.Want to keep only the Field name and the width. Not the starting position and the width.

现有代码的问题是它在一行中打印结果,但我希望每行结束后的\ n.

Problem with existing code is that it prints the result in one single line but i want the \n after the end of each line.

推荐答案

以下脚本(我们称其为 convert.bat )将通过命令行参数给出的文本文件转换为CSV文件.根据您的要求.您可以提供结果文件作为第二个参数.如果省略,输出将显示在控制台上.如果指定了第三个参数,则可以更改默认模式文件 Schema.txt :(因此,使用方式如下: convert.bat source.txt [target.txt [schema.txt]] )

The following script (let us call it convert.bat) converts a text file given via command line argument into a CSV file according to your requirements. You may provide the result file as a second argument; if omitted, the output is displayed at the console. The default schema file Schema.txt can be changed if a third argument is specified: (so use like: convert.bat source.txt [target.txt [schema.txt]])

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Remove leading blanks of every field if this value is non-empty:
set "DELBLANKS=REMOVE"

rem Specify source file as first command line argument:
set "SOURCE=%~1"
rem Specify target file as second argument (optionally):
set "TARGET=%~2"
rem Provide scheme file as third argument (default is "Schema.txt"):
set "SCHEME=%~3"

rem Check the given command line arguments:
if not defined SOURCE >&2 echo ERROR:  no source file given! & exit /B 1
if not defined TARGET set "TARGET=con"
if not defined SCHEME set "SCHEME=%~dp0Schema.txt"

rem Read scheme file and build header:
setlocal EnableDelayedExpansion
set "HEADER="
set /A POSITION=0
set /A COLUMN=0
for /F "usebackq tokens=1,2 delims=," %%I in ("!SCHEME!") do (
    set /A COLUMN+=1
    set "HEADER=!HEADER!,%%I"
    if not "%%J"=="" (
        set "WIDTH=%%J"
        set /A WIDTH[!COLUMN!]+=WIDTH
        set /A POSITION[!COLUMN!]=POSITION
        set /A POSITION+=WIDTH
    )
)

rem Convert source file into CSV format and store to target file:
> "!TARGET!" (
    echo(!HEADER:~1!
    for /F usebackq^ delims^=^ eol^= %%L in ("!SOURCE!") do (
        setlocal DisableDelayedExpansion
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set "LINE=!LINE:,=;!"
        set "CSV="
        for /L %%C in (1,1,%COLUMN%) do (
            for /F "tokens=1,2 delims=," %%P in ("!POSITION[%%C]!,!WIDTH[%%C]!") do (
                if defined DELBLANKS (
                    for /F tokens^=*^ eol^= %%S in ("!LINE:~%%P,%%Q!,") do (
                        for /F "delims=" %%T in (""!CSV!"") do (
                            endlocal
                            set "CSV=%%~T%%S"
                            setlocal EnableDelayedExpansion
                            set "LINE=!LINE:,=;!"
                        )
                    )
                ) else (
                    set "CSV=!CSV!!LINE:~%%P,%%Q!,"
                )
            )
        )
        if defined CSV echo(!CSV:~,-1!
        endlocal
        endlocal
    )
)
endlocal

endlocal
exit /B

模式文件中的标头不应包含任何感叹号.
源文件中的任何逗号 都将由分号; 代替.

The headers in the schema file should not contain any exclamation marks !.
Any commas , in the source file will be replaced by semicolons ;.

这篇关于通用文本转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆