Board logo

标题: [文本处理] BAT怎样把命令结果截取特定内容赋值给一个变量? [打印本页]

作者: xiayukun    时间: 2017-8-17 10:17     标题: BAT怎样把命令结果截取特定内容赋值给一个变量?

本帖最后由 pcl_test 于 2017-8-17 14:41 编辑

(ADDRESS = (PROTOCOL = TCP)(HOST = WIN-6GHP9BD30SV)(PORT = 1521))
    (ADDRESS = (PROTOCOL = TCP)(HOST = WIN-6GHP9BD30SV)(PORT = 1521))

以上为命令结果,怎么获取两个端口号,然后赋值。比如:把两个端口号赋给PORT变量,以空格分开。PORT=1521 1521
作者: Batcher    时间: 2017-8-17 14:24

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "port="
  4. REM 假设你运行的命令是 findstr /c:"PORT =" 1.txt
  5. for /f "delims=" %%a in ('findstr /c:"PORT =" 1.txt') do (
  6.     set str=%%a
  7.     set str=!str:~-15!
  8.     set str=!str:*PORT=!
  9.     set str=!str:~3,-2!
  10.     set port=!port! !str!
  11. )
  12. set port=!port:~1!
  13. echo !port!
  14. pause
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2