搜寻所有数字字符串和鸿沟 [英] Search all numeric string and divide

查看:131
本文介绍了搜寻所有数字字符串和鸿沟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有以下内容的 Output.txt的文件:

I have a Output.txt file which has following content:

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将它们划分在sothat以字节为单位可以变化成MB Newoutput.txt文件存储器

I want to create a batch script which searches for all numeric values in Output.txt (like 54896378,78542,78542 etc.) and divide them by 1024*1024 sothat in Newoutput.txt file memory in BYTES can change into MB.

下面我试过,但没有得到我想要的东西:

I tried below but not getting what I want:

@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只有IE浏览器:

When Output.txt file has following content then everything is working fine but Script is not converting FreePhysicalMemory value 6621212 only i.e.:

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          

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

What changes we need to make in script..?

推荐答案

请注意,批次只有整数有效。数的计算将导致0的MB的值。下面是如何使用十进制值工作的粗糙的例子

Do note that batch only works with integers. Several of your calculations would result in a value of 0 MBs. Here is a rough example of how to work with decimal values.

@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命令。 (下)


    • Update: Added the AppName to the output along with some formatting. (Above)
    • Update: Added Newoutput.txt redirect example. (Above)
    • Update: Added conversion support for all tokens and improved formatting. (Below)
    • Update: Added first line skip fix for the find command. (Below)
    • @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
        

        这篇关于搜寻所有数字字符串和鸿沟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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