0

I'm now on the bigger problem I mentioned in this post...Searching a text file and sending only numbers greater than a certain absolute value to text file?

I figured if I set num=!num:-=! with an original value of, say, -17, then the !errorlevel! will evaluate to true or 0 right? Something's not working here though...

To clarify, I need to filter out only the first and fourth tokens of lines in which the fourth token is either greater than 3 or less than -3, as well as any lines that do not have a 4th token (this part is solved). I have tried using the /A option of set and it doesnt seem to work still.

setlocal enabledelayedexpansion

set "min=-"

for /f "tokens=1,4" %%a in ('findstr /b /r /c:"[^ ]*:S:" print.log') do (
    if %%b=="" (echo %%a ^*^*^* >>new.txt) else (
        set num="%%b"
        set num=!num:-=!
        if !errorlevel!==0 (
            if !num! GTR 3 echo %%a !min!!num! >> new.txt
        ) else (
            if !num! GTR 3 echo %%a !num! >> new.txt
        )
    )
)

exit /b

The text in print.log looks like:

ksdf 0 0 -4

as7d:S:asf 0 0 -4

kc:S:cd3 0 0 -2

asdk:S:s 0 0 6

lasd:S:dd 0 0

Community
  • 1
  • 1
Glycoversi
  • 77
  • 8

3 Answers3

2
@echo off
    setlocal enableextensions disabledelayedexpansion

    >"new.txt" (
        for /f "tokens=1,4" %%a in ('
            findstr /b /r /c:"[^ ]*:S:" print.log
        ') do if "%%~b"=="" (echo %%a ***) else (
            set "print=1" 
            if %%b lss 4 if %%b gtr -4 set "print="
            if defined print echo %%a %%b
        )
    )

Instead of printing when the value is lower than -3 OR greater than 3, it does not print when the value is lower than 4 AND greater than -4

-6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6
          ^.................^
              don't print
MC ND
  • 69,615
  • 8
  • 84
  • 126
0
set /A num=%%b
set num=!num:-=!

This establishes num as the arithmetic value of %%b. Note that environment variables are always strings. The assignment does the conversion each way as required. Your assignment would have assigned a value of (eg) "-4" (including the quotes)

The second command uses the string in num and changes all - to *nothing* hence calculating the absolte-value assuming it's of the appropriate -number structure.

Beyond that, I've no idea what your intentions are to create your output, since you;ve not indicated what the output should be. Either way, errorlevel won't be affected by theset AFAIAA.

Note that %%b will still contain the original number read at the point you are using echo. You can perform an if on %%b as well as on !num! - and use %%b or !num! in your echo as required.

You don't say what your gtr 3 business is designed to do...


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42639932.txt"
FOR /f "tokens=1,4" %%a IN ('findstr /b /r /c:"[^ ]*:S:" "%filename1%"') DO (
 if "%%b"=="" (echo %%a ^*^*^*) else (
  set num=%%b
  set num=!num:-=!
  if !num! GTR 3 (echo %%a %%b 
  ) else (
  ECHO just FOR demo %%a %%b
  )
 )
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q42639932.txt containing your data + extras for my testing.

For each line that passes the findstr filter, if column4 is missing, produce asterisks else set num to the value in %%b, and remove any - from it. If the resultant number is >3, echo the original %%b otherwise - well, I've produced a report line for completeness. Obviously, that can be remmed out.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Just as an academic exercise, it's also possible to get the absolute value of a numeral with [bitwise operations](http://stackoverflow.com/a/12041874/1683264). `set /a "x = %%~b, mask = x >> 31, x = (x ^ mask) - mask"` – rojo Mar 07 '17 at 04:21
  • @Magoo Ive updated my post to clarify my aims. Essentially i want to just grab tokens 1 and 4 from all lines that have a value less than -3 or greater than 3 in the 4th token and send to a file. The /a option doesnt seem to do it. – Glycoversi Mar 07 '17 at 06:50
0
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "threshold=3"
>new.txt (
    for /f "tokens=1,4" %%a in ('findstr /b /r /c:"[^ ]*:S:" print.log') do (
        if "%%b"=="" (
            echo %%a ^*^*^*
        ) else (
            set /A num=%%b
            if !num! GEQ 0 (
                set "min="
            ) else (
                set "num=!num:-=!"
                set "min=-"
            )
            if !num! GTR %threshold% echo %%a !min!!num!
        )
    )
)
JosefZ
  • 28,460
  • 5
  • 44
  • 83