如何将文本文件的所有 CRLF 替换为 ;带有可以使用 Sed、Awk (Gnuwin32) 的 W10 cmd 批处理文件

问题描述 投票:0回答:1

我想在Windows 10下使用Gnuwin32中的sed(或我不太了解的awk)删除每行末尾的CRLF。

我有这个输入(Dos 格式,因此在行尾有 CRLF):

1019<CR><LF>
1020<CR><LF>
1028<CR><LF>
1021<CR><LF>

并且,使用可以使用“W10批处理”或“sed”或“awk”(但不能使用Powershell)的W10 cmd批处理脚本,我想在文本文件中获得以下结果:

1019;1020;1028;1021

结果必须如上:在 1 行上,以半列作为分隔符,并且末尾没有任何半列。

尽管我在这个网站上读过类似的帖子,但我不知道如何做到这一点,因为我总是得到结果

它不适用于批处理文件中的以下几行(似乎有一个带有 GNUwin32 Sed 的 pb 在每个处理行的末尾添加新内容):

REM This to generate the input example :
(echo 1019& echo 1020& echo 1028& echo 1021) > test_in.txt

REM This is the first try for getting the desired 1-line output with semicolumn :
(echo 1019& echo 1020& echo 1028& echo 1021) | .\GnuWin32\bin\sed -e "s/ *$//g" | .\GnuWin32\bin\sed -e "s/\r\n/;/" > test_out.txt

REM This is the second try for getting the desired 1-line output with semicolumn :
REM (echo 1019& echo 1020& echo 1028& echo 1021) | .\GnuWin32\bin\sed -e "s/ *$//g" | .\GnuWin32\bin\sed -b -e "s/\x0d\x0a/;/g" > test_out.txt

REM This is the third try for getting the desired 1-line output with semicolumn :
REM (echo 1019& echo 1020& echo 1028& echo 1021) | .\GnuWin32\bin\sed -e "s/ *$//g" | .\GnuWin32\bin\awk "{gsub(\"\\\\r\\\\n\",\";\")};1" > test_out.txt

REM This is the fourth try for getting the desired 1-line output with semicolumn :
REM (echo 1019& echo 1020& echo 1028& echo 1021) | .\GnuWin32\bin\sed -e "s/ *$//g" | .\GnuWin32\bin\awk -v FS="\r\n" -v OFS=";" -v RS="\\$\\$\\$\\$" -v ORS="\r\n" "{$1=$1}1" > test_out.txt
batch-file awk sed gnuwin32
1个回答
0
投票

在 Unix 中使用 GNU awk 会是:

awk '
    BEGIN { RS="\r\n" }
    { printf "%s%s", (NR>1 ? ";" : ""), $0 }
' file

你如何在 Windows 中调用它,我不知道,但我希望你知道或者可以通过 google 搜索它,因为你正在使用该环境。

© www.soinside.com 2019 - 2024. All rights reserved.