2013-10-17 25 views
1

我有一个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   

什么样的变化,我们需要在脚本做..?

+0

你首先不要在你的新文件中写入任何东西。你也不会对你获得的结果做任何事情。 – Ashalynd

+2

批处理数学在2^31 -1以上,所以你不能处理2GB或更高的值。 – foxidrive

回答

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=" %%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重定向示例。 (以上)
  • 更新:增加了对所有令牌和改进格式的转换支持。 (以下)
  • 更新:为查找命令添加了第一行跳过修复。(如下图)

@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=" %%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 
+0

Thanks.David..its工作作为魅力。:)但是APPNAME不会输出。 – Sunny

+0

@Sunny很高兴能帮到忙,至于APPNAME,你可以举一个你希望如何显示的例子吗?像'APPNAME = 50 MB'? –

+0

David ..就像Output.txt格式.. :) – Sunny

3
SET /A Result = %a/1024*1024 

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

SET /A Result = %a/1024/1024 

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

SET /A Result = %a/1048576 
+0

Stephan..i修改并尝试我的代码根据您的建议..但不知道为什么它不工作。 – Sunny