标题: [文本处理] [已解决]批处理根据文本内容输出对应内容? [打印本页]
作者: czvde 时间: 2022-5-30 11:44 标题: [已解决]批处理根据文本内容输出对应内容?
123.txt:
abcabc,111
cc,0123
abc.txt:
abcabc_abcdef
cc_0123abcd
输出a.txt:
111+ABC.abcdef
0123+ABC.0123abcd
已解决!
作者: flashercs 时间: 2022-5-30 13:28
本帖最后由 flashercs 于 2022-5-30 14:10 编辑
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
- $file1 = "123.txt"
- $file2 = "abc.txt"
- # 格式化字符串
- $format = '{0}+ABC.{1}'
- $ht = @{}
- foreach ($line in (Get-Content -ReadCount 0 -LiteralPath $file1)) {
- $k, $v = $line -split ',', 2
- $ht[$k] = $v
- }
-
- $(foreach ($line in (Get-Content -ReadCount 0 -LiteralPath $file2)) {
- $k, $v = $line -split '_', 2
- if ($ht.ContainsKey($k)) {
- $format -f ($ht[$k]), $v
- }
- }) | Set-Content -LiteralPath .\a.txt
复制代码
作者: Batcher 时间: 2022-5-30 14:22
回复 1# czvde
请参考Q-04和Q-05把bat文件和txt文件都保存为ANSI编码:
https://mp.weixin.qq.com/s/Koje4FufWxWBs7ioDy_LJA- @echo off
- cd /d "%~dp0"
- setlocal enabledelayedexpansion
- for /f "tokens=1-2 delims=," %%i in ('type "123.txt"') do (
- set "%%i=%%j"
- )
- (for /f "tokens=1-2 delims=_" %%i in ('type "abc.txt"') do (
- if defined %%i (
- echo !%%i!+ABC.%%j
- )
- ))>"a.txt"
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |