Board logo

标题: 如何对两个文本中的对应数据进行运算 [打印本页]

作者: xycoordinate     时间: 2007-2-18 02:53    标题: 如何对两个文本中的对应数据进行运算

1.txt
sum1a=1008
sum2a=298
sum3a=157

2.txt
sum1b=2
sum2b=0
sum3b=1

如何编写批处理文件,得到
3.txt
sum1=100900
sum2=29800
sum3=15750



它们的关系是:
sum1=sum1a*100+sum1a*50
sum2=sum2a*100+sum2a*50
sum3=sum3a*100+sum3a*50

[ Last edited by namejm on 2007-2-20 at 02:06 PM ]
作者: xycoordinate     时间: 2007-2-18 02:59
test.bat

@echo off
for /l %%i in (1,1,3) do (
  set sum%%i=0
  set sumA%%i=0
  set sumB%%i=0
  for /f "tokens=2 delims==" %%x in (1.txt) do set /a sumA%%i=%%x*100
  for /f "tokens=2 delims==" %%y in (2.txt) do set /a sumB%%i=%%y*50
  set /a sum%i=sumA%i+sumB%i
  set sum%%i >>3.txt
)

得到的3.txt
sum1=15750
sum2=15750
sum3=15750

???

[ Last edited by xycoordinate on 2007-2-17 at 02:08 PM ]
作者: slore     时间: 2007-2-18 03:57
@echo off

for /f "tokens=1,2 delims==" %%i in (1.txt) do set /a Num%%i=%%j*100
for /f "tokens=1,2 delims==" %%i in (2.txt) do set /a Num%%i=%%j*50

for /l %%i in (1,1,3) do (
set /a sum%%i=Numsum%%ia+Numsum%%ib
set sum%%i >>3.txt
)
作者: jmz573515     时间: 2007-2-18 05:30
强~~
作者: everest79     时间: 2007-2-18 06:37
CODE:  [Copy to clipboard]
@echo off
for /f "tokens=1,2 delims==a" %%a in (1.txt) do call :sss %%a %%b
goto :eof
:sss
for /f "tokens=2 delims==" %%c in ('findstr /b "%~1" 2.txt') do set /a num=%2*100+%%c*50
echo %num%>>3.txt

作者: 3742668     时间: 2007-2-18 07:11
CODE:  [Copy to clipboard]
@echo off
    for /f %%i in (1.txt 2.txt) do set "%%i"
    .
    .
    .
exit /b 0

作者: xycoordinate     时间: 2007-2-18 09:40


  Quote:
Originally posted by everest79 at 2007-2-17 05:37 PM:

@echo off
for /f "tokens=1,2 delims==a" %%a in (1.txt) do call :sss %%a %%b
goto :eof
:sss
for /f "tokens=2 delims==" %%c in ('findstr /b "%~1" 2.txt') do  set /a num=%2*100+%%c*50
echo %num%>>3.txt

DG,你能解释解释吗???

特别的,
findstr /b "%~1" 2.txt

findstr /?
在文件中寻找字符串。

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B        在一行的开始配对模式。
  /E        在一行的结尾配对模式。
  /L        按字使用搜索字符串。
  /R        将搜索字符串作为一般表达式使用。
  /S        在当前目录和所有子目录中搜索
              匹配文件。
  /I         指定搜索不分大小写。
  /X        打印完全匹配的行。
  /V        只打印不包含匹配的行。
  /N        在匹配的每行前打印行数。
  /M        如果文件含有匹配项,只打印其文件名。
  /O        在每个匹配行前打印字符偏移量。
  /P        忽略有不可打印字符的文件。  
  /OFF[LINE] 不跳过带有脱机属性集的文件。
  /A:attr   指定有十六进位数字的颜色属性。请见 "color /?"
  /F:file   从指定文件读文件列表 (/ 代表控制台)。
  /C:string 使用指定字符串作为文字搜索字符串。
  /G:file   从指定的文件获得搜索字符串。 (/ 代表控制台)。
  /D:dir    查找以分号为分隔符的目录列表
  strings   要查找的文字。
  [drive:][path]filename
            指定要查找的文件。

除非参数有 /C 前缀,请使用空格隔开搜索字符串。
例如: 'FINDSTR "hello there" x.y' 在文件 x.y 中寻找 "hello" 或
"there" 。  'FINDSTR /C:"hello there" x.y' 文件 x.y  寻找
"hello there"。

一般表达式的快速参考:
  .        通配符: 任何字符
  *        重复: 以前字符或类别出现零或零以上次数
  ^        行位置: 行的开始
  $        行位置: 行的终点
  [class]  字符类别: 任何在字符集中的字符
  [^class] 补字符类别: 任何不在字符集中的字符
  [x-y]    范围: 在指定范围内的任何字符
  \x       Escape: 元字符 x 的文字用法
  \<xyz    字位置: 字的开始
  xyz\>    字位置: 字的结束

有关 FINDSTR 常见表达法的详细情况,请见联机命令参考。

[ Last edited by xycoordinate on 2007-2-17 at 08:58 PM ]
作者: xycoordinate     时间: 2007-2-18 10:46
谢谢!!!

  Quote:
@echo off
for /f "tokens=1,2 delims==a" %%a in (1.txt) do call :sss %%a %%b
goto :eof
:sss
for /f "tokens=2 delims==" %%c in ('findstr /b "%~1" 2.txt') do  set /a num=%2*100+%%c*50
echo %num%>>3.txt

我终于看懂了!
你将%%a %%b分别看成了%~1 %2
!?

%~1

批参数(%n)的替代已被增强。您可以使用以下语法:

    %~1         - 删除引号("),扩充 %1


但是,如果
1.txt
suma1=1008
suma2=298
suma3=157

2.txt
sumb1=2
sumb2=0
sumb3=1

  Quote:
@echo off
for /f "tokens=1,2 delims=suma,=" %%a in (1.txt) do call :sss %%a %%b
goto :eof
:sss
for /f "tokens=2 delims==" %%c in ('findstr /b "%~1" 2.txt') do  set /a num=%2*100+%%c*50
echo %num%>>3.txt

for命令我又扩展了一点!!!
还有call和goto命令!

everest79
DG,你的观察力那是相当地有穿透力呀!!!!
佩服!

[ Last edited by xycoordinate on 2007-2-17 at 10:55 PM ]
作者: xycoordinate     时间: 2007-2-18 11:56


  Quote:
Originally posted by 3742668 at 2007-2-17 06:11 PM:
CODE:  [Copy to clipboard]
@echo off
    for /f %%i in (1.txt 2.txt) do set "%%i"
    .
    .
    .
exit /b 0

???

DG,你能写详细一点吗???
作者: slore     时间: 2007-2-18 12:02
这个就是直接把sum1a=1008这个set sum1a=1008
明白?都set后可以用for运算了。。
作者: xycoordinate     时间: 2007-2-19 09:33


  Quote:
Originally posted by 3742668 at 2007-2-17 06:11 PM:
CODE:  [Copy to clipboard]
@echo off
    for /f %%i in (1.txt 2.txt) do set "%%i"
    .
    .
    .
exit /b 0

DG,厉害呀!!我实验了一下!!!

@echo off
setlocal enabledelayedexpansion
for /f %%a in (1.txt 2.txt) do set %%a
for /l %%i in (1,1,13) do (
  set /a num%%i=sum%%ia*100+sum%%ib*50
  echo sum%%i=!num%%i! >>3.txt
)
exit /b 0

[ Last edited by xycoordinate on 2007-2-18 at 08:37 PM ]
作者: xycoordinate     时间: 2007-3-4 21:30
《如何对多个文本中的对应数据进行运算》
http://www.cn-dos.net/forum/view ... ge=1&highlight=




欢迎光临 中国DOS联盟论坛 (http://cndos.fam.cx/forum/) Powered by Discuz! 2.5