Scripting 批处理文件-导入变量
例如,我在尝试将以下两行中的:后面的字符串从名为file.txt的文本文件底部转换为两个单独的变量时遇到问题Scripting 批处理文件-导入变量,scripting,batch-file,variables,cmd,Scripting,Batch File,Variables,Cmd,例如,我在尝试将以下两行中的:后面的字符串从名为file.txt的文本文件底部转换为两个单独的变量时遇到问题 Number of files to delete: 27 Total Total size of files to delete: 1,427 KB 我只希望27在一个变量中,1427在另一个变量中。 这些是从扫描中随机生成的,可能包含更多的数字 批处理文件,我使用的是windows,我想你需要在纯bash中完成 #!/bin/bash S="$(<testfile.txt)
Number of files to delete: 27 Total
Total size of files to delete: 1,427 KB
我只希望27在一个变量中,1427在另一个变量中。
这些是从扫描中随机生成的,可能包含更多的数字
批处理文件,我使用的是windows,我想你需要在纯bash中完成
#!/bin/bash
S="$(<testfile.txt)" # load the data
A="${S#*:}" # remove everything before the first : (including it)
A="${A%%Total*}" # remove everything after the first "Total" (including it)
B="${S##*:}" # remove everything before the last : (including it)
B="${B% *}" # remove everything after the last space (including it)
A="${A// }" # remove all spaces
B="${B// }"
echo "-$A-" # print results (showing that there are no spaces in it)
echo "-$B-"
#/bin/bash
S=“$”(类似的方法应该可以:
@echo off
setlocal enableextensions enabledelayedexpansion
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (file.txt) do (
set /a LINES=LINES+1
)
:: Parse the last 2 lines and get the numbers into variable NUMS
set /a LINES=LINES-2
for /f "skip=%LINES% delims=" %%L in (file.txt) do (
for /f "tokens=1-2* delims=:" %%A in ("%%L") do (
for /f "tokens=1-2*" %%N in ("%%B") do set NUMS=!NUMS!%%N;
)
)
:: Parse variable NUMS
for /f "tokens=1-2* delims=;" %%A in ("%NUMS%") do (
set NUM_FILES=%%A
set TOTAL_SIZE=%%B
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
这是我的完整批处理文件
@echo on
setlocal enableextensions enabledelayedexpansion
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (D:\clean.txt) do (
set /a LINES=LINES+1
echo %LINES%
)
:: Parse the last 2 lines and get the numbers into variable NUMS
set /a LINES=LINES-2
echo %LINES%
for /f "skip=%LINES% delims=" %%L in (D:\clean.txt) do (
for /f "tokens=1-2* delims=:" %%A in ("%%L") do (
for /f "tokens=1-2*" %%N in ("%%B") do set NUMS=!NUMS!%%N;
)
)
:: Parse variable NUMS
for /f "tokens=1-2* delims=;" %%A in ("%NUMS%") do (
set NUM_FILES=%%A
set TOTAL_SIZE=%%B
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
你可以看到我把额外的回声放在哪里,也可以看到变量的值,我得到了上一个屏幕截图。为什么它对你有效而对我无效?这不公平,哈哈
我正在运行Vista Home Basic SP1 32位看起来您在读取整个文件以获取行数时遇到了一些问题。此解决方案将只查找您感兴趣的字符串并获取值:
@echo off
setlocal enableextensions enabledelayedexpansion
:: Get number of files to delete.
for /f "usebackq tokens=1-7" %%A in (`findstr /b "Number of files to delete:" file.txt`) do (
set NUM_FILES=%%F
)
:: Get total size of files.
for /f "usebackq tokens=1-8" %%A in (`findstr /b "Total size of files to delete:" file.txt`) do (
set TOTAL_SIZE=%%G
)
@echo Number of files = %NUM_FILES%
@echo Total size of files = %TOTAL_SIZE%
如果包含文件数和总大小的字符串发生变化,则必须相应地更改脚本。jimbob,下面是我的想法:
delfiles.cmd
@echo off
set file=clean.txt
call :grep "Number of files to delete:"
set files=%grep%
call :grep "Total size of files to delete:"
set size=%grep%
echo %Files% files of a total size of %size% are to be deleted.
exit /b 0
:grep
setlocal
for /F "tokens=2 delims=:" %%i in ('findstr /i /c:"%~1" "%file%"') do (
for /F "tokens=1 delims= " %%j in ("%%i") do set _find=%%j
)
endlocal& set grep=%_find%
exit /b 0
这是一个可重用的脚本,因为文件和字符串可以用相同的结果进行更改
@Patrick Cuff:如果找不到/c:“…”部分,你可能会遇到问题。可能是问题的一部分,但也许我的脚本会给出相同的结果/问题…我想,现场学习吧
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
设置文件=:将其设置为要扫描的文件的文件(以及路径,如果需要)。
调用:grep“string”:使用要查找的字符串调用:grep函数。
设置变量=%grep%:将变量(此处为文件和大小)设置为:grep的答案
:grep函数:它首先在%file%中查找“%string%”,然后在“:”字符处对其进行分析,保留右侧部分。然后使用“spaces”再次对其进行分析,并保留第一个单词。该函数返回包含找到的字符串的变量%grep%
与我通常的回答一样,我希望这能有所帮助。我已经运行了一些测试,这个脚本(删除了echo
后)在我的盒子上以3500次/s的速度执行,当加载文件只执行一次时,以22000次/s的速度执行。期待使用head、tail、cut和cat的其他解决方案;)这看起来是一个很好的bash答案,但我认为他可能在Windows上。请参阅batch和cmd标记echo%line%
for
循环中的for
行在执行for
命令之前进行计算,因此将始终显示为零。要查看行
是否正在递增,请使用延迟扩展;echo!行!
。除此之外,您的脚本对我有效。我没有访问Vista系统的权限,但我将在Windows 7上运行此脚本,并查看发生的情况。这两个脚本对我在Windows 7上都有效。请查看我的最新答案以了解其他可能的解决方案。