gpt4 book ai didi

batch-file - 如果最后一个字符是使用批处理文件的分号,则将行与下一行合并

转载 作者:行者123 更新时间:2023-12-03 16:42:45 26 4
gpt4 key购买 nike

我有一个包含以下 4 行的文件。

A;1;abc;<xml/>;
;2;def;<xml
>hello world</xml>;
;3;ghi;<xml/>;

使用批处理文件,我需要合并行,以便如果该行不以分号 (;) 结尾,则将下一行合并到当前行中。

所以期望的输出应该是
A;1;abc;<xml/>;
;2;def;<xml>hello world</xml>;
;3;ghi;<xml/>;

我对批处理脚本不是很熟悉,但尝试使用 for /F但到目前为止还没有运气。

据我了解,逻辑应该是检查每一行的最后一个字符,如果它不是分号,则将下一行读入当前行。

除此之外,我设法获得了该行的最后一个字符,但我的脚本仅在不存在时读取该行; .有任何想法吗?
@echo off
for /f "tokens=*" %%i in (myfile.txt) do (
set var=%%i
echo %%i
if "%var:~-1%"==";" (
echo test
)
)

注意:上面的查询只读取第 1 行和第 3 行。

最佳答案

你的代码有很多问题:)

1) 正如您所说,您的代码忽略以 ; 开头的行- 这是由于默认的 FOR/F EOL 选项。但是由于“TOKENS=*”,您的代码还会从每一行中删除前导空格。您需要将 EOL 和 DELIMS 都设置为空。语法很奇怪,但它有效:

for /f delims^=^ eol^= %%i ...

2)您尝试在带括号的代码块中设置和扩展 var 。这是行不通的,因为在解析行时会发生扩展,并且会立即解析整个代码块。所以 %var% 的值是循环执行之前存在的值。当然不是你想要的。解决方案是使用延迟扩展。输入 FOR /?从命令提示符获取有关延迟扩展的更多信息(大约在帮助列表的一半处)

3) 对于包含 ! 的变量内容如果在启用延迟扩展时扩展它会损坏。解决方案是在循环中根据需要打开和关闭延迟扩展。但这会导致复杂性,因为您需要在 ENDLOCAL 屏障上保留生长线的值。我使用 FOR/F 将值传输穿过障碍。

这是一个完整的批处理脚本,应该可以完成这项工作。它的局限性在于它不能处理大于 ~8191 字节的最大长度的行。

此代码已被重新​​编写以修复一个重大错误
@echo off
setlocal disableDelayedExpansion
set "ln="
set "print=0"
for /f delims^=^ eol^= %%i in (myfile.txt) do (
set "var=%%i"
setlocal enableDelayedExpansion
for /f delims^=^ eol^= %%A in ("!ln!!var!") do (
if "!var:~-1!"==";" (
endlocal
echo %%A
set "ln="
) else (
endlocal
set "ln=%%A"
)
)
)

SET/P 解决方案

有一个更简单的解决方案可以立即打印每一行,这样您就不必担心跨 ENDLOCAL 传输变量。不以 ; 结尾的行使用 SET/P 打印不带换行符。

此解决方案具有以下限制:

1) 通过 SET/P 打印的行将去掉前导空格。此限制仅适用于 Vista 和更新版本的 Windows。在 XP 上这不是问题。

2) 感谢 David Ruhmann,我现在知道如果该行以 = 开头,SET/P 将失败.非常不幸:(
@echo off
setlocal disableDelayedExpansion
set "ln="
for /f delims^=^ eol^= %%i in (myfile.txt) do (
set "var=%%i"
setlocal enableDelayedExpansion
if "!var:~-1!"==";" (echo !var!) else (<nul set /p ="!var!")
endlocal
)

混合批处理/JScript 正则表达式解决方案(防弹?)

我编写了一个混合批处理/JScript REPL.BAT 实用程序,该实用程序允许简单的正则表达式搜索和替换文件内容。它使工作变得非常容易。

以下命令适用于任何输入,没有限制。它已更新为支持 Windows 和 Unix 风格的行。而且它比纯批处理解决方案要快得多。
findstr "^." myfile.txt|repl "([^;\r])\r?\n" "$1" m >"outFile.txt"

这是 REPL.BAT 实用程序。完整的文档嵌入在脚本中。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::************ Documentation ***********
:::
:::REPL Search Replace [Options [SourceVar]]
:::REPL /?
:::
::: Performs a global search and replace operation on each line of input from
::: stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: If called with a single argument of /? then prints help documentation
::: to stdout.
:::
::: Search - By default this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript syntax documentation is available at
::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
::: Replace - By default this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substituion patterns available to the JScript replace method.
::: A $ literal can be escaped as $$. An empty replacement string
::: must be represented as "".
:::
::: Replace substitution pattern syntax is documented at
::: http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
::: Options - An optional string of characters used to alter the behavior
::: of REPL. The option characters are case insensitive, and may
::: appear in any order.
:::
::: I - Makes the search case-insensitive.
:::
::: L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in Replace are
::: treated as $ literals.
:::
::: E - Search and Replace represent the name of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: M - Multi-line mode. The entire contents of stdin is read and
::: processed in one pass instead of line by line. ^ anchors
::: the beginning of a line and $ anchors the end of a line.
:::
::: X - Enables extended substitution pattern syntax with support
::: for the following escape sequences:
:::
::: \\ - Backslash
::: \b - Backspace
::: \f - Formfeed
::: \n - Newline
::: \r - Carriage Return
::: \t - Horizontal Tab
::: \v - Vertical Tab
::: \xnn - Ascii (Latin 1) character expressed as 2 hex digits
::: \unnnn - Unicode character expressed as 4 hex digits
:::
::: Escape sequences are supported even when the L option is used.
:::
::: S - The source is read from an environment variable instead of
::: from stdin. The name of the source environment variable is
::: specified in the next argument after the option string.
:::

::************ Batch portion ***********
@echo off
if .%2 equ . (
if "%~1" equ "/?" (
findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" ""
exit /b 0
) else (
call :err "Insufficient arguments"
exit /b 1
)
)
echo(%~3|findstr /i "[^SMILEX]" >nul && (
call :err "Invalid option(s)"
exit /b 1
)
cscript //E:JScript //nologo "%~f0" %*
exit /b 0

:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b

************* JScript portion **********/
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) {
options+=args.Item(2).toLowerCase();
}
var multi=(options.indexOf("m")>=0);
var srcVar=(options.indexOf("s")>=0);
if (srcVar) {
options=options.replace(/s/g,"");
}
if (options.indexOf("e")>=0) {
options=options.replace(/e/g,"");
search=env(search);
replace=env(replace);
}
if (options.indexOf("l")>=0) {
options=options.replace(/l/g,"");
search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");
replace=replace.replace(/\$/g,"$$$$");
}
if (options.indexOf("x")>=0) {
options=options.replace(/x/g,"");
replace=replace.replace(/\\\\/g,"\\B");
replace=replace.replace(/\\b/g,"\b");
replace=replace.replace(/\\f/g,"\f");
replace=replace.replace(/\\n/g,"\n");
replace=replace.replace(/\\r/g,"\r");
replace=replace.replace(/\\t/g,"\t");
replace=replace.replace(/\\v/g,"\v");
replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g,
function($0,$1,$2){
return String.fromCharCode(parseInt("0x"+$0.substring(2)));
}
);
replace=replace.replace(/\\B/g,"\\");
}
var search=new RegExp(search,options);

if (srcVar) {
WScript.Stdout.Write(env(args.Item(3)).replace(search,replace));
} else {
while (!WScript.StdIn.AtEndOfStream) {
if (multi) {
WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace));
} else {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace));
}
}
}

关于batch-file - 如果最后一个字符是使用批处理文件的分号,则将行与下一行合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com