【求助】怎样用批处理批量替换ttxt里",1=",这里1的数字长度不一定是1,不是固定长度,怎样将其中的=和,替换

比如txt文档里有
a,1=这是第一行
bb,2=这是第二行
ccc,3=
这是第三行
dddd,4=这是第四行
.....

怎样批量替换成
aaa=1,这是第一行
bbb=2,这是第二行
ccc=3,
这是第三行
ddd=4,这是第四行
.....

第1个回答  2020-04-27
不清楚你的实际文件/情况,仅以问题中的样例/说明为据
复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起运行<# :
cls
@echo off
rem 将一个指定txt文本文件内每一行中的部分特定内容调换位置
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$path=$args[0];
$txtfile=$path+'yourfile.txt';
if(-not (test-path -liter $txtfile)){Write-host ('"'+$txtfile+'" not found');exit;};
$enc=[Text.Encoding]::Default;
[System.Collections.ArrayList]$s=@();
$text=[IO.File]::ReadAllLines($txtfile, $enc);
for($i=0;$i -lt $text.count;$i++){
    $m=[regex]::match($text[$i], '^(.+?)(,)([^=]+?)(=)(.*)$');
    $line=$text[$i];
    if($m.Success){
        $line=$m.groups[1].value+$m.groups[4].value+$m.groups[3].value+$m.groups[2].value+$m.groups[5].value;
    };
    [void]$s.add($line);
    write-host $line;
}

追问

不好意思回复晚了,之前忙着工作,一时半会忘了,您的代码我测试了,确实互换了逗号和等号,但原文件没修改,请问怎样将cmd上的结果生成新txt保存呢?

追答

按运行后窗口标题栏中的提示联系我

本回答被网友采纳
第2个回答  2020-04-28
D:\test>type 1.txt 2.txt

1.txt

a,1=这是第一行
bb,2=这是第二行
ccc,3=
这是第三行
dddd,4=这是第四行系统找不到指定的文件。
处理: 2.txt 时出错。

D:\test>for /f "tokens=1-3 delims=,=" %i in (1.txt) do @echo %i=%j,%k
a=1,这是第一行
bb=2,这是第二行
ccc=3,
这是第三行=,
dddd=4,这是第四行

D:\test>type 1.bat
@echo off
(for /f "tokens=1-3 delims=,=" %%i in (1.txt) do @echo %%i=%%j,%%k
)>2.txt
D:\test>call 1

D:\test>type 2.txt
a=1,这是第一行
bb=2,这是第二行
ccc=3,
这是第三行=,
dddd=4,这是第四行
相似回答