2025-03-01 07:34:30
以下是一种实现方式,可以将其保存为.bat批处理文件执行:
bashCopy code@echo off
setlocal enabledelayedexpansionset "findstrPattern=[0-9.]+E[+-][0-9]+"set "replacementPattern=--------"for /f "delims=" %%i in (input.txt) do ( set "line=%%i"
set "firstTwo=!line:~0,15!"
set "rest=!line:~15!"
set "replaced=!rest:%findstrPattern%=%replacementPattern%!"
echo !firstTwo!!replaced!
)
其中input.txt为数据文件名,可以根据实际情况修改。该批处理文件的具体实现方法如下:
首先设置了一个findstrPattern变量和一个replacementPattern变量,分别用于匹配要替换的文本和替换后的符号。
使用for循环遍历输入文件的每一行数据。
在循环中,首先将整行数据存储到line变量中。
接着使用!line:~0,15!获取每行的前两个数,存储到firstTwo变量中。
然后使用!line:~15!获取从第三个数开始到行末的字符串,存储到rest变量中。
使用!rest:%findstrPattern%=%replacementPattern%!替换rest变量中所有匹配findstrPattern变量的字符串为replacementPattern变量中的符号,将替换后的字符串存储到replaced变量中。
最后使用echo输出firstTwo和replaced变量拼接后的字符串。
这个批处理文件中使用了延迟变量扩展,即在for循环中使用!括起变量名。这是因为在循环中修改变量时,如果使用%括起变量名,循环会一次性解析完所有变量后再开始执行,这样会导致变量的值无法正确更新。使用!括起变量名可以在每次循环中动态地解析变量,从而正确地更新变量的值。
2025-03-01 11:42:39
2025-03-01 01:09:59
层主,这个实现了保留前两行,但没有实现非1.00E+00的数据替换
2025-03-01 11:19:40
<# :
cls&echo off&cd /d "%~dp0"&mode con lines=5000
rem 将一个txt文本文件内每一行指定特征的字段中的每个字符替换成指定字符
path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
set #=Any questions&set _=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
set "current=%cd%"
powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
echo;%#% +%$%%$%/%_% %z%
pause
exit
#>
$current=($env:current).trimend('\');
$files=@(dir -literal $current|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
if($files.length -ge 1){
$enc=New-Object System.Text.UTF8Encoding $False;
$text=[IO.File]::ReadAllLines($files[0], $enc);
for($i=0;$i -lt $text.count;$i++){
$m=[regex]::match($text[$i], '^(\s*?\S+\s+\S+)(\s+\S.*)$');
if($m.Success){
$text[$i]=$m.groups[1].value+[regex]::replace($m.groups[2].value, '\S+', {
param($r);
if($r.groups[0].value -ne '1.0E+00'){
return ($r.groups[0].value -replace '\S','-');
}else{return $r.groups[0].value;}
});
}
$text[$i];
}
}
2025-03-01 12:02:33