搜索所有数字字符串并除以

时间:2013-10-17 14:02:03

标签: batch-file for-loop

我有一个Output.txt文件,其中包含以下内容:

Server1
APPNAME  MEMORY
WINDOWS  54896378
LINUX    78542
MACOS    187963

Server2
APPNAME     MEMORY
DATABASE    587412369
SCHEMA      78542
TABLESPACE  187963

我想创建一个批处理脚本,搜索Output.txt中的所有数值(如54896378,78542,78542等),并将它们除以1024 * 1024,以便在BYTES的Newoutput.txt文件内存中更改为MB

我在下面尝试但没有得到我想要的东西:

@echo off
setlocal enabledelayedexpansion
for /F "delims= " %%a in ('findstr "[1-9][0-9]* 0"' Output.txt) do (
SET /A Result = %a / 1024*1024 > Newoutput.txt
)

EDIT1

Output.txt文件具有以下内容时,一切正常,但脚本不会仅转换FreePhysicalMemory值6621212,即:

Output.txt的:

Server1
APPNAME  MEMORY
WINDOWS  54896378
LINUX    78542
MACOS    187963

FreePhysicalMemory  TotalVisibleMemorySize  
6621212                 8387172   

Newoutput.txt:

Server1
APPNAME  MEMORY
WINDOWS  13.58
LINUX    2.45
MACOS    1.8

FreePhysicalMemory  TotalVisibleMemorySize  
6621212                 21.4          

我们需要在脚本中进行哪些更改..?

2 个答案:

答案 0 :(得分:3)

SET /A Result = %a / 1024*1024

这将%a除以1024并将输出乘以1024.这不是你想要的。 正确的划分应该是:

SET /A Result = %a / 1024 / 1024

或者您可以预先计算1024 * 1024 = 1048576和

SET /A Result = %a / 1048576

答案 1 :(得分:3)

请注意,批处理仅适用于整数。您的一些计算将导致0 MB的值。以下是如何使用十进制值的粗略示例

@echo off
call :Parse > Newoutput.txt
exit /b 0

:Parse
for /f "tokens=1,2" %%A in (Output.txt) do call :ToMB "%%~B" "%%~A" || echo(%%A %%B
exit /b 0

:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
exit /b 0

:ToMB <String> <Name>
setlocal
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number=   %Number%"
set "Name=%~2            "
echo %Name:~0,12%%Number:~-3%.%Decimal:~-3%
endlocal
exit /b 0

  • 更新:将AppName添加到输出中以及一些格式。 (上图)
  • 更新:添加了Newoutput.txt重定向示例。 (上图)
  • 更新:为所有令牌添加了转换支持并改进了格式。 (下)
  • 更新:为find命令添加了第一行跳过修复。 (下)

@echo off
call :Parse > Newoutput.txt
exit /b 0

:Parse
setlocal
for /f "tokens=1,* delims=]" %%A in ('type "Output.txt" ^|find /n /v ""') do (
    for /f "tokens=1,2" %%X in ("%%~B") do call :Convert "%%~X" "%%~Y"
    call :Blank "%%~B"
)
endlocal
exit /b 0

:Blank <String>
set "String=%~1"
if not defined String echo.
exit /b 0

:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
if "%~1"=="" exit /b 2
exit /b 0

:Convert <String> <String>
call :Calculate "%~1" Y || call :Display "%~1" Y
call :Calculate "%~2" || call :Display "%~2"
echo.
exit /b 0

:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number=000%Number%"
call :Display "%Number:~-3%.%Decimal:~-3%" %2
exit /b 0

:Display <String> [Pad]
set "String=%~1"
set "Pad=%~2"
if defined Pad set "String=%String%                        "
if defined String set /p "=%String:~0,24%" <nul
exit /b 0

  • 更新:添加PowerShell以计算例程以处理最多2 ^ 64(下方)
  • 的值

:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number="
set "Decimal="
for /f "tokens=1,2 delims=." %%A in ('"PowerShell %~1 / ( 1024 * 1024 )"') do (
    set "Number=%%A"
    set "Decimal=%%B000"
)
call :Display "%Number%.%Decimal:~0,3%" %2
exit /b 0