好友
阅读权限 20
听众
最后登录 1970-1-1
200 吾爱币
本帖最后由 lcldh 于 2024-9-5 14:22 编辑
实现方式:批处理 / EmEditor / notepad++
用AI生成的,请测试好再发,别直接生成就上来贴答案!浪费大家时间
文件路径:D/1
修改:D/1目录下的所有TXT
从第1行开始, 每行的前3个AAA分别替换为&1@ 一1@ 二1@ ,后面两个AAA不变。
每加1行,中间的数字加1数,
替换到到第31行后从第1行开始循环
&1@ 一1@ 二1@
&2@ 一2@ 二2@
...
...
...
&31@ 一31@ 二31@
&1@ 一1@ 二1@
--------------------
原文示例:
{ afas: 'AAA925dgsfgdd这是第1行', yiojhkhb: '39I1F0HTBU', asdsA: 'VDLHTR', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '502144:L3B7JH', hjgAd: 'a',asd, , u: '437NRWHQaBMPRaS368aT478aYX0GLQBK', gfhfg: 'a'}<brab1111df />{ afas: 'AAA929dgsfgdd变量文字示例', yiojhkhb: '1XAYE3AECK', asdsA: '7M3YV3', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '827557:XCTOLT', hjgAd: 'a',asd, , u: 'Q1350HRMaL2D8a2JUOa3KVPaJVXZTBLG', gfhfg: 'a'}<brab1111df />{ afas: 'AAA600dgs夫', yiojhkhb: 'V6BCIZVGH3', asdsA: 'FP4V2I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '037571:5FULS8', hjgAd: 'a',asd, , u: 'BZ0FY2B9aXKM0aD13HaE23Ia5SU8SV52', gfhfg: 'a'}<brab1111df />{ afas: 'AAA739dgsfgdd变量文字-第1行结束', yiojhkhb: '8LRJ52215U', asdsA: 'FZ0N15', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '066267:5PQDRV', hjgAd: 'a',asd, , u: 'X6OO7DL5aKNX4a04DKa15ELaQ0HH06FY', gfhfg: 'a'}<brab1111df />{ afas: 'AAA245dgsfgdd变量文字示例', yiojhkhb: 'Z4GYY924YF', asdsA: 'A3PVWU', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '973555:0TFLLK', hjgAd: 'a',asd, , u: 'I4LXFX51aISJ9abZQa090RaCYER9RZU', gfhfg: 'a'
{ afas: 'AAA389dgsfgdd这是第2行', yiojhkhb: 'ZLJ7ZD609H', asdsA: 'YEJO9I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '501381:O49EZ8', hjgAd: 'a',asd, , u: 'D2I6EIFNaS87Qa9ON7aAPO8a7VB07B9H', gfhfg: 'a'}<brab1111df />{ afas: 'AAA86dgsfgdd变量文字示例', yiojhkhb: '54WWEKTDQC', asdsA: 'HSVXR8', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '145548:7ILNHY', hjgAd: 'a',asd, , u: 'Z9EGM2ZJaDPGJaU6WZaV7X0aT389GWTD', gfhfg: 'a'}<brab1111df />{ afas: 'AAA433dgsfgdd变量文字示例', yiojhkhb: 'S5D53ZCAPE', asdsA: 'JE3QS6', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '107348:94TGIW', hjgAd: 'a',asd, , u: 'K7BPUN96a0JQMaHZ73aI084aE15IOH3Z', gfhfg: 'a'}<brab1111df />{ afas: 'AAA704dgsfgdd变量文字示例', yiojhkhb: 'MQNV77MNUA', asdsA: 'QT20OX', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '347635:GJSQEN', hjgAd: 'a',asd, , u: '658Y38J2aZXDSaGET8aHEU9a0Z2SW1DV', gfhfg: 'a'}<brab1111df />{ afas: 'AAA621dgsfgdd变量文字-第2行结束', yiojhkhb: 'XHSFJX2VHD', asdsA: 'FFY4DW', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '006705:55OU3M', hjgAd: 'a',asd, , u: '1C571J3Oa9319aPKHPaQLIQaV6Z1VCWI', gfhfg: 'a'
...
...
{ afas: 'AAA9025dgsfgdd这是第31行', yiojhkhb: '39I1F0HTBU', asdsA: 'VDLHTR', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '502144:L3B7JH', hjgAd: 'a',asd, , u: '437NRWHQaBMPRaS368aT478aYX0GLQBK', gfhfg: 'a'}<brab1111df />{ afas: 'AAA929dgsfgdd变量文字示例', yiojhkhb: '1XAYE3AECK', asdsA: '7M3YV3', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '827557:XCTOLT', hjgAd: 'a',asd, , u: 'Q1350HRMaL2D8a2JUOa3KVPaJVXZTBLG', gfhfg: 'a'}<brab1111df />{ afas: 'AAA600dgs夫', yiojhkhb: 'V6BCIZVGH3', asdsA: 'FP4V2I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '037571:5FULS8', hjgAd: 'a',asd, , u: 'BZ0FY2B9aXKM0aD13HaE23Ia5SU8SV52', gfhfg: 'a'}<brab1111df />{ afas: 'AAA739dgsfgdd变量文字示例', yiojhkhb: '8LRJ52215U', asdsA: 'FZ0N15', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '066267:5PQDRV', hjgAd: 'a',asd, , u: 'X6OO7DL5aKNX4a04DKa15ELaQ0HH06FY', gfhfg: 'a'}<brab1111df />{ afas: 'AAA245dgsfgdd变量文字-第31行结束', yiojhkhb: 'Z4GYY924YF', asdsA: 'A3PVWU', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '973555:0TFLLK', hjgAd: 'a',asd, , u: 'I4LXFX51aISJ9abZQa090RaCYER9RZU', gfhfg: 'a'
运行后的结果:
{ afas: '&1@ 925dgsfgdd这是第1行', yiojhkhb: '39I1F0HTBU', asdsA: 'VDLHTR', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '502144:L3B7JH', hjgAd: 'a',asd, , u: '437NRWHQaBMPRaS368aT478aYX0GLQBK', gfhfg: 'a'}<brab1111df />{ afas: '一1@ 929dgsfgdd变量文字示例', yiojhkhb: '1XAYE3AECK', asdsA: '7M3YV3', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '827557:XCTOLT', hjgAd: 'a',asd, , u: 'Q1350HRMaL2D8a2JUOa3KVPaJVXZTBLG', gfhfg: 'a'}<brab1111df />{ afas: '二1@ 600dgsfgdd变量文字示例', yiojhkhb: 'V6BCIZVGH3', asdsA: 'FP4V2I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '037571:5FULS8', hjgAd: 'a',asd, , u: 'BZ0FY2B9aXKM0aD13HaE23Ia5SU8SV52', gfhfg: 'a'}<brab1111df />{ afas: 'AAA739dgsfgdd变量文字示例', yiojhkhb: '8LRJ52215U', asdsA: 'FZ0N15', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '066267:5PQDRV', hjgAd: 'a',asd, , u: 'X6OO7DL5aKNX4a04DKa15ELaQ0HH06FY', gfhfg: 'a'}<brab1111df />{ afas: 'AAA245dgsfgdd变量文字示例', yiojhkhb: 'Z4GYY924YF', asdsA: 'A3PVWU', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '973555:0TFLLK', hjgAd: 'a',asd, , u: 'I4LXFX51aISJ9abZQa090RaCYER9RZU', gfhfg: 'a'
{ afas: '&2@ 389dgsfgdd这是第2行', yiojhkhb: 'ZLJ7ZD609H', asdsA: 'YEJO9I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '501381:O49EZ8', hjgAd: 'a',asd, , u: 'D2I6EIFNaS87Qa9ON7aAPO8a7VB07B9H', gfhfg: 'a'}<brab1111df />{ afas: '一2@ 86dgsfgdd变量文字示例', yiojhkhb: '54WWEKTDQC', asdsA: 'HSVXR8', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '145548:7ILNHY', hjgAd: 'a',asd, , u: 'Z9EGM2ZJaDPGJaU6WZaV7X0aT389GWTD', gfhfg: 'a'}<brab1111df />{ afas: '二2@ 433dgsfgdd变量文字示例', yiojhkhb: 'S5D53ZCAPE', asdsA: 'JE3QS6', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '107348:94TGIW', hjgAd: 'a',asd, , u: 'K7BPUN96a0JQMaHZ73aI084aE15IOH3Z', gfhfg: 'a'}<brab1111df />{ afas: 'AAA704dgsfgdd变量文字示例', yiojhkhb: 'MQNV77MNUA', asdsA: 'QT20OX', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '347635:GJSQEN', hjgAd: 'a',asd, , u: '658Y38J2aZXDSaGET8aHEU9a0Z2SW1DV', gfhfg: 'a'}<brab1111df />{ afas: 'AAA621dgsfgdd变量文字示例', yiojhkhb: 'XHSFJX2VHD', asdsA: 'FFY4DW', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '006705:55OU3M', hjgAd: 'a',asd, , u: '1C571J3Oa9319aPKHPaQLIQaV6Z1VCWI', gfhfg: 'a'
...
...
...
...
{ afas: '& 31@ 25dgsfgdd这是第31行', yiojhkhb: '39I1F0HTBU', asdsA: 'VDLHTR', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '502144:L3B7JH', hjgAd: 'a',asd, , u: '437NRWHQaBMPRaS368aT478aYX0GLQBK', gfhfg: 'a'}<brab1111df />{ afas: '一31@ 929dgsfgdd变量文字示例', yiojhkhb: '1XAYE3AECK', asdsA: '7M3YV3', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '827557:XCTOLT', hjgAd: 'a',asd, , u: 'Q1350HRMaL2D8a2JUOa3KVPaJVXZTBLG', gfhfg: 'a'}<brab1111df />{ afas: '二31@ 600dgsfgdd变量文字示例', yiojhkhb: 'V6BCIZVGH3', asdsA: 'FP4V2I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '037571:5FULS8', hjgAd: 'a',asd, , u: 'BZ0FY2B9aXKM0aD13HaE23Ia5SU8SV52', gfhfg: 'a'}<brab1111df />{ afas: 'AAA', yiojhkhb: '8LRJ52215U', asdsA: 'FZ0N15', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '066267:5PQDRV', hjgAd: 'a',asd, , u: 'X6OO7DL5aKNX4a04DKa15ELaQ0HH06FY', gfhfg: 'a'}<brab1111df />{ afas: 'AAA', yiojhkhb: 'Z4GYY924YF', asdsA: 'A3PVWU', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '973555:0TFLLK', hjgAd: 'a',asd, , u: 'I4LXFX51aISJ9abZQa090RaCYER9RZU', gfhfg: 'a'
{ afas: '&1@ 925dgsfgdd这是第1行', yiojhkhb: '39I1F0HTBU', asdsA: 'VDLHTR', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '502144:L3B7JH', hjgAd: 'a',asd, , u: '437NRWHQaBMPRaS368aT478aYX0GLQBK', gfhfg: 'a'}<brab1111df />{ afas: '一1@ 929dgsfgdd变量文字示例', yiojhkhb: '1XAYE3AECK', asdsA: '7M3YV3', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '827557:XCTOLT', hjgAd: 'a',asd, , u: 'Q1350HRMaL2D8a2JUOa3KVPaJVXZTBLG', gfhfg: 'a'}<brab1111df />{ afas: '二1@ 600dgsfgdd变量文字示例', yiojhkhb: 'V6BCIZVGH3', asdsA: 'FP4V2I', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '037571:5FULS8', hjgAd: 'a',asd, , u: 'BZ0FY2B9aXKM0aD13HaE23Ia5SU8SV52', gfhfg: 'a'}<brab1111df />{ afas: 'AAA739dgsfgdd变量文字示例', yiojhkhb: '8LRJ52215U', asdsA: 'FZ0N15', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '066267:5PQDRV', hjgAd: 'a',asd, , u: 'X6OO7DL5aKNX4a04DKa15ELaQ0HH06FY', gfhfg: 'a'}<brab1111df />{ afas: 'AAA245dgsfgdd变量文字-第1行结束', yiojhkhb: 'Z4GYY924YF', asdsA: 'A3PVWU', fytutfrhg: '', : 'abc', hhh: 'fjgfg', ahjgh: '973555:0TFLLK', hjgAd: 'a',asd, , u: 'I4LXFX51aISJ9abZQa090RaCYER9RZU', gfhfg: 'a'
@lixiaoyao666 大佬来~
------------------------
AI半成品 replace_pre_tags.ps1 # 遍历 D:/1 目录下的所有 TXT 文件 Get-ChildItem -Path "D:/1" -Filter *.txt | ForEach-Object { $filePath = $_.FullName $lines = Get-Content -Path $filePath # 初始化行号计数器 $lineCounter = 1 $replaceCounter = 1 # 循环处理每一行 $newLines = $lines | ForEach-Object { # 定义要替换的标记及格式 if ($lineCounter -le 31) { # 替换前 3 个 AAA $_ -replace "AAA", "&$replaceCounter@", 1 ` -replace "AAA", "一$replaceCounter@", 1 ` -replace "AAA", "二$replaceCounter@", 1 $replaceCounter++ } else { # 超过 31 行后重置计数器 $replaceCounter = 1 $_ -replace "AAA", "&$replaceCounter@", 1 ` -replace "AAA", "一$replaceCounter@", 1 ` -replace "AAA", "二$replaceCounter@", 1 $replaceCounter++ } $lineCounter++ } # 将修改后的内容写回文件 Set-Content -Path $filePath -Value $newLines } Write-Host "替换完成!"
最佳答案
查看完整内容
import os
input_folder = r"C:\path\to\input_folder"# 替换为实际的输入文件夹路径
output_folder = r"C:\path\to\output_folder" # 替换为实际的输出文件夹路径
if not os.path.exists(output_folder):
os.makedirs(output_folder)
def process_line(line, line_num):
count = 0
while 'AAA' in line and count < 3:
count += 1
if count == 1:
line = line.replace ...