![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-29 08:53 |
47,816 topics / 349,916 posts / today 0 new / 48,265 members |
| DOS批处理 & 脚本技术(批处理室) » If we find the maximum value of a set of temperature record values in 1 text and make a judgment |
| Printable Version 2,700 / 20 |
| Floor1 quan_zhou | Posted 2007-05-12 14:41 |
| 初级用户 Posts 40 Credits 190 | |
|
If you want to find the maximum value of a set of temperature records in a text and make a judgment: if it is greater than 45 and less than 90, display OK; otherwise, display NG. The running environment is XP SP2, and it can be achieved with batch processing or VBS scripts. Please help take a look. I tried with FOR but it didn't work. It should be that I don't know how to use it.
The text content is as follows: --------1.txt--------------- New logging session started at : 04:57:57 Your product Upper Temperature Limit is : 115 C. 04:57:57 | CT : 76 C AT : 0 C 04:58:02 | CT : 76 C AT : 0 C 04:58:07 | CT : 76 C AT : 0 C 04:58:12 | CT : 76 C AT : 0 C 04:58:17 | CT : 76 C AT : 0 C 04:58:22 | CT : 76 C AT : 0 C 04:58:27 | CT : 76 C AT : 0 C 04:58:32 | CT : 76 C AT : 0 C 04:58:37 | CT : 76 C AT : 0 C 04:58:42 | CT : 76 C AT : 0 C 04:58:47 | CT : 76 C AT : 0 C 04:58:52 | CT : 76 C AT : 0 C 04:58:57 | CT : 76 C AT : 0 C 04:59:02 | CT : 76 C AT : 0 C 04:59:07 | CT : 77 C AT : 0 C 04:59:12 | CT : 77 C AT : 0 C 04:59:17 | CT : 83 C AT : 0 C 04:59:22 | CT : 77 C AT : 0 C 04:59:27 | CT : 77 C AT : 0 C 04:59:32 | CT : 77 C AT : 0 C 04:59:37 | CT : 77 C AT : 0 C 04:59:42 | CT : 82 C AT : 0 C 04:59:47 | CT : 78 C AT : 0 C 04:59:52 | CT : 86 C AT : 0 C 04:59:57 | CT : 89 C AT : 0 C 05:00:02 | CT : 90 C AT : 0 C 05:00:07 | CT : 91 C AT : 0 C 05:00:12 | CT : 93 C AT : 0 C 05:00:17 | CT : 93 C AT : 0 C 05:00:22 | CT : 94 C AT : 0 C 05:00:27 | CT : 95 C AT : 0 C 05:00:32 | CT : 95 C AT : 0 C 05:00:37 | CT : 96 C AT : 0 C 05:00:42 | CT : 97 C AT : 0 C 05:00:47 | CT : 97 C AT : 0 C 05:00:52 | CT : 98 C AT : 0 C 05:00:57 | CT : 98 C AT : 0 C 05:01:02 | CT : 99 C AT : 0 C 05:01:07 | CT : 100 C AT : 0 C 05:01:12 | CT : 100 C AT : 0 C 05:01:17 | CT : 102 C AT : 0 C 05:01:22 | CT : 105 C AT : 0 C 05:01:27 | CT : 106 C AT : 0 C 05:01:32 | CT : 108 C AT : 0 C 05:01:37 | CT : 101 C AT : 0 C 05:01:42 | CT : 99 C AT : 0 C 05:01:48 | CT : 101 C AT : 0 C 05:01:53 | CT : 102 C AT : 0 C 05:01:58 | CT : 110 C AT : 0 C 05:02:03 | CT : 112 C AT : 0 C 05:02:08 | CT : 114 C AT : 0 C 05:02:13 | CT : 113 C AT : 0 C 05:02:18 | CT : 114 C AT : 0 C 05:02:23 | CT : 114 C AT : 0 C 05:02:28 | CT : 114 C AT : 0 C 05:02:33 | CT : 116 C AT : 0 C 05:02:38 | CT : 116 C AT : 0 C 05:02:43 | CT : 117 C AT : 0 C 05:02:48 | CT : 118 C AT : 0 C 05:02:53 | CT : 119 C AT : 0 C ----------------------------------------------------- The longer the temperature monitoring is, the more content there is in the text. [ Last edited by quan_zhou on 2007-5-12 at 03:44 PM ] |
|
| Floor2 wudixin96 | Posted 2007-05-12 15:41 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
```@echo off&setlocal enabledelayedexpansion
set a=0 for /f "tokens=5" %%i in (1.txt) do if %%i GTR !a! set a=%%i if %a% GTR 45 (goto label) else (echo NG) :label if %a% LSS 90 (echo OK) else (echo NG) pause ``` |
|
| Floor3 wudixin96 | Posted 2007-05-12 15:45 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
If the text has
New logging session started at : 04:57:57 Your product Upper Temperature Limit is : 115 C. Should skip 4 lines. |
|
| Floor4 quan_zhou | Posted 2007-05-12 16:01 |
| 初级用户 Posts 40 Credits 190 | |
|
Thank you very much for brother wudixin96's reply. Your code I tested is OK, thank you! Actually, I have been in contact with DOS for a long time, but I can't go deep into it. It's really useless! Can you help explain your code?
|
|
| Floor5 wudixin96 | Posted 2007-05-12 16:10 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
@echo off&setlocal enabledelayedexpansion
set a=0 /*Used to store the maximum temperature value and initialize*/ for /f "skip=4 tokens=5" %%i in (1.txt) do if %%i GTR !a! set a=%%i /*skip=4 skips 4 lines. Since tokens=5, the temperature value is stored in %%i. You can enter for /? in cmd to learn about the usage of for*/ if %a% GTR 45 (goto label) else (echo NG) :label if %a% LSS 90 (echo OK) else (echo NG) pause I can't explain anymore. My writing skills are very poor. Sorry ^-^ |
|
| Floor6 lxmxn | Posted 2007-05-12 16:15 |
| 版主 Posts 4,938 Credits 11,386 | |
|
```gawk "/CT/{if($5>45&&$5<90){print \"OK\"} else {print \"NG\" }}" 1.txt```
|
|
| Floor7 wudixin96 | Posted 2007-05-12 16:18 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
Is gawk something related to Linux?
|
|
| Floor8 zhoushijay | Posted 2007-05-12 16:26 |
| 高级用户 Posts 375 Credits 845 | |
|
set fso=createobject("scripting.filesystemobject")
set op=fso.opentextfile("1.txt",1) set d=createobject("scripting.dictionary") on error resume next for i=1 to 1000 op.skipline ln=op.readline if len(ln)=39 then t=mid(ln,17,2) end if if len(ln)=40 then t=mid(ln,17,3) end if t=int(t) if err=0 then d.add i,t end if next for each i in d if d(i)>b then mx=d(i) end if b=d(i) next if mx>45 and mx<90 then msgbox("Temperature is OK"&chr(10)&"The hotest is "&mx&"C") else msgbox("Temperature is NG"&chr(10)&"The hotest is "&mx&"C") end if VBS version ^^! |
|
| Floor9 quan_zhou | Posted 2007-05-12 17:06 |
| 初级用户 Posts 40 Credits 190 | |
|
TO zhoushijay Brother
After your code runs, it always shows "The hotest is* C", which is not the maximum temperature value in the text. I tried changing the content in 1.TXT several times, and it's always like that. Thank you everyone for your replies. TO wudixin96k Brother Thank you for your explanation. May I ask if GTR in your code means less than and LSS means greater than? Because I tested it separately and it didn't work. For example: @echo off set a=99 if %a% GTR 45 goto min if %a% LSS 90 goto max :min Display test less than 45 goto end :max Display test greater than 90 goto end :end |
|
| Floor10 zhoushijay | Posted 2007-05-12 17:12 |
| 高级用户 Posts 375 Credits 845 | |
|
Yeah, I also found the problem and am working hard to fix it.
|
|
| Floor11 everest79 | Posted 2007-05-12 17:14 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
set /a 1/(var/45)&& set /a 1/(90/var)&& echo %var%
|
|
| Floor12 lxmxn | Posted 2007-05-12 17:18 |
| 版主 Posts 4,938 Credits 11,386 | |
Originally posted by wudixin96 at 2007-5-12 16:18: Linux only integrates this tool. Actually, it can be regarded as a programming language, a professional programming language for text processing. gawk is the GNU version of awk. To learn more about awk in detail, you can take a look here: http://www.itisedu.com/phrase/200603021801255.html |
|
| Floor13 slore | Posted 2007-05-12 17:22 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
Set fso = CreateObject("Scripting.FileSystemObject")
Set op = fso.OpenTextFile("1.txt",1) Set d = CreateObject("Scripting.Dictionary") on Error Resume Next 'Try to use less... For i To 1000 'It is recommended to use AtEndOfStream... so that the above error ignoring is not needed and it can handle text with more than 1000 lines op.skipline 'Why skip here before readline? ln = op.ReadLine If Len(ln) = 39 Then 'Use len and mid below... It is recommended to learn to use Instr... Otherwise, when the length is not only 2 types... t = Mid(ln,17,2) End If If Len(ln) = 40 Then t = Mid(ln,17,3) End If t = Int(t) If Err = 0 Then d.add i,t 'Still the old problem, why not use array d(i)=t, I don't think using Dictionary object has any advantages End If Next For Each i In d 'Using Dictionary, how to become array, sweat~ If d(i) > b Then mx = d(i) End If b = d(i) Next If mx > 45 And mx < 90 Then MsgBox("Temperature is OK" & Chr(10) & "The hotest is " & mx & "C") 'Use constants instead of using functions. Chr(10)=vbCr Else MsgBox "Temperature is NG" & Chr(10) & "The hotest is " & mx & "C" 'Don't use parentheses when no return value is needed End If '--Lack of releasing objects-- Set d = Nothing Set op = Nothing Set fso = Nothing [ Last edited by slore on 2007-5-12 at 05:36 PM ] |
|
| Floor14 lxmxn | Posted 2007-05-12 17:25 |
| 版主 Posts 4,938 Credits 11,386 | |
|
```gawk "BEGIN{max=0}/CT/{if($5>max)max=$5;if($5>45&&$5<90){print \"OK\"} else {print \"NG\" }}END{print \"The max tep is\",max}" 1.txt```
|
|
| Floor15 wudixin96 | Posted 2007-05-12 17:30 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
EQU - equals
NEQ - not equals LSS - less than LEQ - less than or equal to GTR - greater than GEQ - greater than or equal to Check out "if /?" in cmd? |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |