平面文件源,没有换行或回车

时间:2016-12-10 20:02:57

标签: batch-file scripting

我有一个平面文件来源,没有换行或回车。有五个字符串#@#@#,表示它是行尾,即

abc|def|123#@#@#xyz|tuv|567#@#@#

我需要通过informatica处理此文件,但我相信我无法使用该五个字符串设置行分隔符。

我需要编写一个批处理脚本(.bat)来用换行符替换该字符串的所有出现。我搜索了很多,但还没找到任何结果。谁知道我怎么处理它? 我已经看到了一些使用" delims ="的解决方案,尽管这会逐行读取文件。在我的情况下,由于缺少换行符,它只有一行,因此解决方案不起作用。

输出文件看起来像:

abc|def|123
xyz|tuv|567

2 个答案:

答案 0 :(得分:4)

在powershell控制台中输入

(gc InFile.txt -raw) -replace "#@#@#", "`r`n"|set-content OutFile.txt  

替换InFile.txt,OutFile.txt以满足您的需求

答案 1 :(得分:2)

我不知道为什么我之前让事情变得如此复杂 - 请参阅下面的原始答案......

无论如何,以下代码片段应该可以满足您的需求:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument)
set "_SEQU=#@#@#"
rem // Define line-break:
(set _LF=^
%= empty line =%
)

rem // Read specified input file:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
    rem // Store line string:
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    rem // Replace all sequences by line-breaks:
    echo(!LINE:%_SEQU%=^%_LF%%_LF%!
    endlocal
)

endlocal
exit /B

原始答案

这是一个纯粹的解决方案 - 请参阅所有解释性rem备注:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument)
set "_SEQU=#@#@#"

rem // Read specified input file:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
    rem // Store line string:
    set "LINE=%%L"
    setlocal EnableDelayedExpansion
    rem // Double quotation marks temporarily:
    set "LINE=!LINE:"=""!^"
    rem /* Put quotation marks around the entire line string, then replace every
    rem    found sequence by a space in between quotation marks; this results in
    rem    a string in which each proposed output line is placed in between a pair
    rem    of quotation marks and separated from each other by a single space: */
    set LINE="!LINE:%_SEQU%=" "!"
    rem // Provide the resulting string as argument string for a sub-routine:
    call :PROCESS !LINE!
    endlocal
)

endlocal
exit /B


:PROCESS
setlocal DisableDelayedExpansion
rem // Establish a loop over all given (quoted) arguments:
:LOOP
rem // Check current argument against emptiness, meaning that the end is reached:
set ARG=%1
if not defined ARG endlocal & exit /B
rem // Store the argument with the surrounding quotation marks removed:
set "ARG=%~1"
setlocal EnableDelayedExpansion
rem // Reverse doubling of carets done by the sub-routine call:
if defined ARG set "ARG=!ARG:^^=^!"
rem // Reverse doubling of quotation marks:
if defined ARG set "ARG=!ARG:""="!^"
rem // Return the resulting output line string:
echo(!ARG!
endlocal
rem // Move over to next argument:
shift
goto :LOOP