问题描述
我知道很多次都问过类似的问题,但是我无法锻炼适合自己需求的东西...
I know similar questions had been asked many time, but I cannot workout something fitting my needs...
所以我有一个文件,其中包含一个密钥,该行的开头是:
So I have a file, containing a secret key, the line is starting like :
SECRET_KEY =
密钥的长度是固定的.
我想找到此行,并将其保存在某处,将包含密钥的所有文件替换为另一个文件(非常相似,但使用我不想使用的其他密钥),并重新引入原始文件新文件中的秘密密钥!
I want to find this line, to save it somewhere, to replace the all file containing the secret key by another file (very similar, but with a different secret key which I don't want), and to reintroduce the original secret key in the new file!
所以目前我有以下代码:
So for the moment I have this code:
echo Saving the actual SECRET_KEY...
findstr "SECRET_KEY" somefile.py > SECRET_KEY.txt
echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY
这部分工作正常,因为我已将原始密钥存储在一个文本文件中,并用另一个文件(以及其他文件,目录结构等)替换了该文件
This part is working, as I have stored the original secret key in a text file, and replaced the file by another (together with other files, directories structures, etc...)
那么现在我如何才能再次找到以SECRET_KEY开头的行,并将所有行替换为SECRET_KEY.txt的内容?
So now how can I find again the line starting by SECRET_KEY and replace the all line by the content of SECRET_KEY.txt?
我认为我取得了一些进步:
I made some progress I think:
echo Saving the SECRET_KEY...
findstr "SECRET_KEY" inputfile.py > SECRET_KEY.txt
echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY
echo Restoring the SECRET_KEY...
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('findstr /I "SECRET_KEY" SECRET_KEY.txt') do set "secret_key=%%a"
echo !secret_key!
@echo off
set file=inputfile.py
set newline=!secret_key!
set insertline=23
set output=outputfile.py
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
if "%%~a"=="%insertline%" (
echo !newline!
) ELSE (
echo.%%b
)
)) > %output%
然后我可以删除SECRET_KEY.TXT,inputfile.py,并将outputfile.py重命名为inputfile.py很有可能不是最有效的方法,但是它可以工作...
I can then delete SECRET_KEY.TXT, inputfile.py, and rename outputfile.py into inputfile.pyMost likely not the most efficent way, but it works...
除了一件事!我在此输入文件中有很多特殊字符,这就是为什么我使用!secret_key!的原因.例如...
EXCEPT for one thing! I have a lot a special characters in this inputfile, that's why I used !secret_key! for example...
但是我也有很多:
[some stuff]
并在输出文件中显示为:
and in the outputfile, it appears as:
[some stuff
所以我想我已经接近了,但是我找不到一个允许输出每个字符的定界符!
So I guess I'm close, but I can't find a delimiter which will allow every single character to be outputed!
推荐答案
以下脚本首先从文件otherfile.py
中读取包含SECRET_KEY
的行;然后获取输入文件inputfile.py
所在行的编号,其中SECRET_KEY
放置在该行中;然后逐行读取文件inputfile.py
,检查每行号是否与先前找到的相符;如果找到匹配项,则返回先前从文件otherfile.py
收集的密钥,因此该行将被替换;否则,该行返回未编辑状态;输出数据写入文件outputfile.py
:
The following script reads the line containing SECRET_KEY
from file otherfile.py
at first; then it gets the number of the line of the input file inputfile.py
where SECRET_KEY
is placed; afterwards it reads the file inputfile.py
line by line, check each line number whether it matches the previously found one; if a match is found, the previously gathered key from file otherfile.py
is returned, so the line is replaced; otherwise, the line returned unedited; the output data is written to file outputfile.py
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "INFILE=inputfile.py"
set "OUTFILE=outputfile.py"
set "KEYFILE=otherfile.py"
set "KEYWORD=SECRET_KEY"
rem // Retrieve key from a file:
set "KEY="
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%K in ('findstr /L /C:"!KEYWORD!" "%KEYFILE%"') do (
endlocal
set "KEY=%%K"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem // `goto` ensures to take *first* key; remove it to take *last* one;
goto :CONT_KEY
)
:CONT_KEY
endlocal
rem // Retrieve line number where key is found:
set "LRPL="
setlocal EnableDelayedExpansion
for /F "delims=:" %%M in ('findstr /N /L /C:"!KEYWORD!" "%INFILE%"') do (
endlocal
set "LRPL=%%M"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem // `goto` ensures to take *first* key; remove it to take *last* one;
goto :CONT_NUM
)
:CONT_NUM
endlocal
rem // Move line by line from one file to another and replace key:
> "%OUTFILE%" (
rem /* Use `findstr` to precede every line with its line number plus `:`,
rem in order no line to appear empty to `for /F`, as this ignores such;
rem the prefix (up to and including the first `:`) is removed later: */
for /F "delims=" %%L in ('findstr /N /R "^" "%INFILE%"') do (
set "LINE=%%L"
rem // Extract the prefixed number of the current line:
for /F "delims=:" %%N in ("%%L") do set "LNUM=%%N"
rem // Toggle delayed expansion inside of the loop to not lose `!`:
setlocal EnableDelayedExpansion
rem /* Check current line number against previously retrieved one
rem and replace line in case of a match: *//
if !LNUM! EQU !LRPL! (echo(!KEY!) else (echo(!LINE:*:=!)
endlocal
)
)
endlocal
exit /B
请注意,KEYFILE
和INFILE
变量(在脚本顶部定义)可以设置为相同的文件路径.但是,OUTFILE
必须与INFILE
不同;否则,引用的文件将被清空.
Note that the KEYFILE
and INFILE
variables (defined at the top of the script) may be set to the same file path. However, OUTFILE
must be different from INFILE
; otherwise the referred file will be emptied.
这仅涉及以下任务:将文件的SECRET_KEY
行存储为变量,以及将文件中包含SECRET_KEY
的行与先前存储的文件交换,并将结果写入另一个文件.
This covers only the tasks of storing the SECRET_KEY
line of a file to a variable and of exchanging the line containing SECRET_KEY
in a file by the previously stored one and write the result to another file.
这篇关于批处理:用另一行替换一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!