【问题标题】:flat file source with no line feed or carriage return没有换行或回车的平面文件源
【发布时间】:2016-12-10 20:02:56
【问题描述】:

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

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

我需要通过informatica处理这个文件,但我相信我不能用那五个字符串设置行分隔符。

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

输出文件应该是这样的:

abc|def|123
xyz|tuv|567

【问题讨论】:

  • 你应该先自己尝试一些东西,当你卡住的时候回到这里,清楚地描述你有什么问题;否则这是一个为我工作的问题,在这里是题外话!请至少阅读完整的tour page 以了解如何使用本网站!
  • 您的输入文件是否包含以下字符之一:"*?
  • 它是一个数据文件,因此可以在文件的某处看到这些字符。但是,如果您要问文件是否在每个字段中都包含这些字符,那么我的答案是否定的,
  • 在 powershell 控制台中输入 (gc InFile.txt -raw) -replace "#@#@#", "rn"|set-content OutFile.txt 查看答案,格式化会吃掉转义字符。
  • 在@LotPings 的代码中,"`r`n" 中有反引号,这些反引号很难用 Stack Overflow 降价格式显示。你也可以(gc infile.txt) -split '#@#@#' > outfile.txt

标签: batch-file scripting


【解决方案1】:

在powershell控制台中输入

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

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

【讨论】:

  • 我会试一试,然后告诉你结果。非常感谢 LotPings。
【解决方案2】:

我不知道为什么我之前把事情弄得这么复杂——请看下面的原始答案...

无论如何,下面的代码 sn-p 应该做你想做的事:

@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

【讨论】:

  • 嗨 aschipfl。再次感谢您的回答。此更新后的代码能够读取最多 8184 个字符的多行。当文件具有更多字符时,它不会做任何事情。我知道它试图一次阅读所有内容。谢谢
  • 不客气!使用for /F逐行读取文件,每行限制为8191个字符/字节;另一个限制来自set "LINE=%%L" 行,即 8184 字节,即 8191 – 4 – 1 – 2,其中 4 是变量名的长度,1 是等号,它们都存储在环境块,当然还有字符串值;第三个限制也可能经常计算——命令行的长度,特别是在立即 (%VAR%) 扩展的情况下。不幸的是,您只能通过使用另一种语言来避免这些限制...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 2010-10-30
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2015-12-16
相关资源
最近更新 更多