Austin Burk
Austin Burk

Reputation: 960

How can I split a string with batch commands in Windows?

I have an LED sign set up with a Windows computer, and am trying to make it display my Linux computer's temperature:

acpitz-virtual-0

Adapter: Virtual device

temp1: +97.7°F (crit = +183.2°F)

Now, here's the batch file on my computer

wget 192.168.1.58/sensor1.txt

I have a windows version of wget in the folder.

type sensor1.txt | findstr /v acpitz-virtual-0 | findstr /v Adapter: > msg.txt
set /p msg= < msg.txt

prismcom.exe usb {HOLD} %msg%

Now my sign flashes the equivalent of

temp1: +97.7°F (crit = +183.2°F)

I need it to flash

+97.7°F,

or even better,

+97.7 F

I've been trying with FIND and FOR and commands like that, but with no luck. How can I modify my string to work?

Thanks!

Upvotes: 0

Views: 603

Answers (2)

BDM
BDM

Reputation: 3900

Say the variable that the temperature is stored in is called "temp", and has the value of "temp1: +97.7°F (crit = +183.2°F)". This little snippet should work wonders...

setlocal enabledelayedexpansion
set dis=!temp:~7,5! F
set check=!temp:~10,1!
if %check% neq . set dis=!temp:~7,6! F

Here is the syntax:

set variable=!variable_to_be_constrained:~offset,amount_of_characters!

One thing I should mention is that the variable "TEMP" (or "temp", capitalization generally doesn't matter too much with .bat files) is a predefined command line variable, and generally those should never be overwritten. However, in this case it's purely for aesthetic purposes and you can change it to whatever you want.

--EDIT-- Added two lines to allow for temperatures over 100 Fahrenheit. Also changed temp to dis.

Upvotes: 4

Gene
Gene

Reputation: 46960

If you have a Windows version of wget, then you might as well also have a special command to parse the temperature. In C you can easily do what you want:

#include <stdio.h>

int main(void) {
    char buf[1000];
    scanf("temp1: %[+0-9.]", buf);
    printf("%sF", buf);
    return 0;
}

Compile this with any C compiler. Obtain a free one from MinGW (preferred because the compiled code won't require any extra DLLs) or Cygwin (which does require an extra DLL). You can also try Microsoft Visual Studio Express, but it's more hassle.

If the code above is in filter.c, then the command for MinGW or Cygwin will be

gcc filter.c -o filter

This will produce a file called filter.exe, which you can use in your pipeline with

type sensor1.txt | findstr /v acpitz-virtual-0 | findstr /v Adapter: | filter > msg.txt

The moral is that limited tools (like cmd.exe) are often not worth the trouble.

Upvotes: 0

Related Questions