Remove char and CR/LF



我有一个文本文件,我需要在特定情况下删除托架返回/线供稿。我不想全部删除它们。

我知道我可以运行代码以删除CR/LF。但是,我有一个文件,我只想删除cr/lf,如果它在a/

之前

看起来像这样(插入正确的斑点):

"2016-09-11 23:22:03","20<CR/LF>
16-09-11 >03:22:24",20160911,1,16,21,281,281,4272,4272,NULL,NULL,NULL,0,2100,2528,NULL<CR/LF>

因此,我不希望最后一个示例中的最后一个示例。

预先感谢

这是一个纯(评论得很好的)批处理解决方案 - 假设最终的串联线不超过约8190个字符或字节:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (first command line argument specifies the input text file)
set "_CHAR=/"   & rem // (character that marks line concatenation)
rem // Reset buffer for concatenation:
set "CONC="
rem /* Read input text file line by line; since `for /F` ignores empty lines, use `findstr`
rem    to prefix them by their line numbers and a colon, so they do not appear empty: */
for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
    rem // Store current line, including line number prefix:
    set "LINE=%%L"
    rem // Toggle delayed expansion to avoid loss of exclamation marks:
    setlocal EnableDelayedExpansion
    rem // Check whether last character is the predefined concatenation mark:
    if "!LINE:~-1!"=="%_CHAR%" (
        rem // Contatenation required, so remove line number prefix and concatenate:
        set "LINE=!LINE:*:=!"
        set "CONC=!CONC!!LINE:~,-1!"
    ) else (
        rem /* No concatenation needed, so output current line with line number prefix
        rem    removed and prefixed by current concatenation buffer: */
        echo(!CONC!!LINE:*:=!
        rem // Reset concatenation buffer:
        set "CONC="
    )
    rem /* Transfer concatenation buffer beyond the environment localisation barrier
    rem    (this is needed because of `endlocal` and toggling delayed expansion): */
    for /F "delims=" %%K in (^""!CONC!"^") do (
        endlocal
        set "CONC=%%~K"
    )
)
rem // Output remaining concatenation buffer, if there is something left:
if defined CONC (
    setlocal EnableDelayedExpansion
    echo(!CONC!
    endlocal
)
endlocal
exit /B

这将/用作标记字符。要将其更改为 ,请转到脚本顶部的注释Define constants here:引入的块。

9dan的建议,我通过记事本 分为三个步骤。

  1. 将所有CR/LF通过REGEX更改为XX
  2. 通过常规
  3. 将 xx更改为"(空白)
  4. 将xx更改为cr/lf通过正则

它正常工作。

不确定如何接受9dan的答案,只是想分享。

最新更新