批处理文件以从文本中过滤字符串并替换...但是是单个批处理而不是多个批处理? [英] Batch file to filter strings from text and replace... but in a single batch instead of multiple?

查看:91
本文介绍了批处理文件以从文本中过滤字符串并替换...但是是单个批处理而不是多个批处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下几行的.txt:

I have a .txt with the following lines:

#307714 Joana Darc Vitoria Campos 2013-07-14 14:25:27 0.0000
#322521 ALINE MADALENA 2013-07-16 08:31:11 0.0000
#323057 Juliana E Ferreira Araujo 2013-07-16 09:35:36 0.0000
#325581 DIOGO 2013-07-16 14:12:12 0.0000
#326718 Matheus 2013-07-16 16:29:51 0.0000

我已经编写了以下代码,该代码将从上面的文本中过滤出#号并将其替换为另一批!!

And i have made the following code which will filter # from text above and replace the # in a separate batch!

第一个过滤#的代码如下:

So the first one to filter the # is as follows:

@echo off
if exist id.txt (
    for /f "tokens=1 delims= " %%a in (id.txt) do (
    findstr /c:"%%a" id.txt >nul && (echo %%a >> trials.txt | echo Filtered: %%a to trials.txt)
    )
) else (
    Echo id.txt não existe.. cria o ficheiro e cola lá  a lista de refs para filtragem!
    pause >nul
)

它将过滤ID,并将其放置在名为trials.txt的文本文件中!

It will filter the ID's and place them in a text file named trials.txt!

然后,我用此代码创建了一个单独的批处理,以将#替换为链接:

Afterwards i created a separated batch with this code to replace # with a link:

@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=trials.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=#
set REPLACETEXT=Give trial:
set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
    SET string=%%A
    setlocal EnableDelayedExpansion
    SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

    >> %OUTTEXTFILE% echo(!modified!
    endlocal
)
del /q %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

运行两个批次后的最终结果将是:

And the final result after running the two batches will be:

Give trial:307714  
Give trial:322521  
Give trial:323057  
Give trial:325581  
Give trial:326718

任何人都知道将一个批处理文件制作成一个文件的任何方法吗?

Anyone know any way to make these two batch files in a single one?

谢谢:)

推荐答案

for /f "tokens=1 delims=# " %%a in (id.txt) do (
 >>trials.txt echo(#%%a
 >>test_out.txt echo(Give trial:%%a
)

真的不知道您是否需要trials.txtid.txt是否仅包含与#nnn ...匹配的行

Don't really know whether you need trials.txt or whether id.txt contains ONLY lines matching #nnn ...

这篇关于批处理文件以从文本中过滤字符串并替换...但是是单个批处理而不是多个批处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆