批量查找和替换特定文本的脚本,然后在 .txt 文件中的替换文本后添加 3 个空格
Script to batch find and replace specific text then add 3 spaces after the replaced text in .txt file
我对脚本编写还很陌生,并且在 Internet 上四处搜索,但似乎找不到针对我要实现的目标的特定解决方案,所以我希望有人能提供一些帮助。
我有一个 .txt 文件,其中包含各种数据行,这些数据按从特定列号开始的文本组织 - 基本上是 table 数据。请参阅下面的示例,其中显示了每列的起始位置:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|MD-3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Please click here for screenshot if above example makes no sense
我希望脚本在第 620 列中找到包含 'MD-' 的所有行并将其删除,以便只留下数字。所以我 运行 PowerShell 中的 Replace 命令确实删除了所有包含 'MD-' 的行,但是它没有对齐其余的列;
使用的 PowerShell 命令:
(Get-Content "test.txt") |
Foreach-Object {$_.replace("MD-", "")} |
Set-Content "testedited.txt"
以上命令的输出:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Click here for screenshot if above example makes no sense
如您所见,“+12345678912”不再与第 632 列对齐,'MDABWPX0001000001' 不再与第 672 列对齐。
有没有办法在不影响其他列的情况下执行上述命令?我在某处读到 Hash tables 可以做到这一点,但是我并不完全理解该方法。
期望的输出:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Please click here to see screenshot of desired output
我愿意使用任何脚本语言/方法来执行此任务,因此非常感谢任何建议。
在此先感谢您。
使用 -replace 正则表达式很容易做到这一点。我从第 11 列开始用 'MD' 进行了测试。将其更改为 620,或任何需要的值。
(Get-Content "test.txt") |
ForEach-Object { $_ -replace '^(.{11})MD\-([^ ]*|)(.*)$', ' ' } |
Set-Content "testedited.txt"
这里是测试数据和样本运行。
PS C:\src\t\repmd> Get-Content .\test.txt
0123456789|asdf |asdfdsaf
0123456789|MD-333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
PS C:\src\t\repmd> .\repmd.ps1
PS C:\src\t\repmd> Get-Content .\testedited.txt
0123456789|asdf |asdfdsaf
0123456789|333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
正则表达式分解成这样。
^ beginning of string
(.{11}) capture 1 - eleven (11) characters
MD\- literal 'MD-' (the '-' character needs to be escaped with \
([^ ]*|) capture 2 - all non-space characters until a VERTICAL LINE
(.*) capture 3 - all remaining characters
$ end of string
' '
生成捕获的字符串。 $3 前的三 (3) 个空格替换 'MD-'.
取出的三 (3) 个字符
$_ -replace '(?<=.{620})MD-([0-9]+)',(''+' '*3)
@echo off
setlocal EnableDelayedExpansion
rem Change next line by 620 and 12
set /A "pos=11, wide=8"
set /A "posP3=pos+3, rest=wide-3, posPwide=pos+wide"
(for /F "delims=" %%a in (Input.txt) do (
set "line=%%a"
if "!line:~%pos%,3!" equ "MD-" (
set "line=!line:~0,%pos%!!line:~%posP3%,%rest%! !line:~%posPwide%!"
)
echo !line!
)) > Output.txt
Input.txt:
0123456789|asdf |asdfdsaf
0123456789|MD-333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
Output.txt:
0123456789|asdf |asdfdsaf
0123456789|333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
我对脚本编写还很陌生,并且在 Internet 上四处搜索,但似乎找不到针对我要实现的目标的特定解决方案,所以我希望有人能提供一些帮助。
我有一个 .txt 文件,其中包含各种数据行,这些数据按从特定列号开始的文本组织 - 基本上是 table 数据。请参阅下面的示例,其中显示了每列的起始位置:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|MD-3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Please click here for screenshot if above example makes no sense
我希望脚本在第 620 列中找到包含 'MD-' 的所有行并将其删除,以便只留下数字。所以我 运行 PowerShell 中的 Replace 命令确实删除了所有包含 'MD-' 的行,但是它没有对齐其余的列;
使用的 PowerShell 命令:
(Get-Content "test.txt") |
Foreach-Object {$_.replace("MD-", "")} |
Set-Content "testedited.txt"
以上命令的输出:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Click here for screenshot if above example makes no sense
如您所见,“+12345678912”不再与第 632 列对齐,'MDABWPX0001000001' 不再与第 672 列对齐。
有没有办法在不影响其他列的情况下执行上述命令?我在某处读到 Hash tables 可以做到这一点,但是我并不完全理解该方法。
期望的输出:
| | |
|1214000 |1234567890 |ISRBWPX0001000001
| | |
|3300 |+12345678912 |MDABWPX0001000001
| | |
| | |
| | |
Col:620 Col:632 Col:672
Please click here to see screenshot of desired output
我愿意使用任何脚本语言/方法来执行此任务,因此非常感谢任何建议。
在此先感谢您。
使用 -replace 正则表达式很容易做到这一点。我从第 11 列开始用 'MD' 进行了测试。将其更改为 620,或任何需要的值。
(Get-Content "test.txt") |
ForEach-Object { $_ -replace '^(.{11})MD\-([^ ]*|)(.*)$', ' ' } |
Set-Content "testedited.txt"
这里是测试数据和样本运行。
PS C:\src\t\repmd> Get-Content .\test.txt
0123456789|asdf |asdfdsaf
0123456789|MD-333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
PS C:\src\t\repmd> .\repmd.ps1
PS C:\src\t\repmd> Get-Content .\testedited.txt
0123456789|asdf |asdfdsaf
0123456789|333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
正则表达式分解成这样。
^ beginning of string
(.{11}) capture 1 - eleven (11) characters
MD\- literal 'MD-' (the '-' character needs to be escaped with \
([^ ]*|) capture 2 - all non-space characters until a VERTICAL LINE
(.*) capture 3 - all remaining characters
$ end of string
' '
生成捕获的字符串。 $3 前的三 (3) 个空格替换 'MD-'.
$_ -replace '(?<=.{620})MD-([0-9]+)',(''+' '*3)
@echo off
setlocal EnableDelayedExpansion
rem Change next line by 620 and 12
set /A "pos=11, wide=8"
set /A "posP3=pos+3, rest=wide-3, posPwide=pos+wide"
(for /F "delims=" %%a in (Input.txt) do (
set "line=%%a"
if "!line:~%pos%,3!" equ "MD-" (
set "line=!line:~0,%pos%!!line:~%posP3%,%rest%! !line:~%posPwide%!"
)
echo !line!
)) > Output.txt
Input.txt:
0123456789|asdf |asdfdsaf
0123456789|MD-333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf
Output.txt:
0123456789|asdf |asdfdsaf
0123456789|333 |asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqwasda|asdfdsaf
0123456789|cwqw |asdfdsaf
0123456789|cwqw |asdfdsaf