user1313051
user1313051

Reputation: 39

Batch - Using findstr to read a file entry and output if it is less than a variable

I'm trying to read a text file using findstr to output only specific characters based on whether the value is less than my predefined number value. Here is a line from the text file that i am trying to read:

BW6556 *NULL* 1 6 1406922741 1407046596 1415081796 0 845292618 3 3 5 5 0 8 1024

BW6657 *NULL* 1 6 1408817016 1408817016 1416852216 0 193816666 2 2 5 5 0 0 1024

I need an expression that looks at the above line and does the following:

  1. looks at the long number on each line that starts at character 41 and ends on character 50
  2. If that number is GREATER THAN 1 and LESS THAN my variable number %expirydate% then write out characters 1-7 to a new file.
  3. Does the same thing on every line in the text file!

Any ideas?

Upvotes: 1

Views: 1026

Answers (3)

npocmaka
npocmaka

Reputation: 57252

There are many questions..You want the number BEFORE or AFTER spaces are cleared?Are you sure that the these are exact end and start charaters ? What can be max value of the numbers ? Can it be more than 2147483647 - the max integer value?If it is - can be a string comparison be executed instead?

@echo off

set /a variable=2147483647
set "val_file=number.txt"
set "new_file=new.txt"

break>"%new_file%"
setlocal enableDelayedExpansion
for /f "useback delims=" %%# in ("%val_file%") do (
    set "line=%%#"
    set line=!line:~40,10!
    set /a number=!line: =!
    rem echo !number! !variable!
    if !number! GTR 1 if !number! LSS !variable! (echo(!number!)>>"%new_file%"

)

Upvotes: 0

dbenham
dbenham

Reputation: 130819

That cannot be done with FINDSTR. First off, FINDSTR cannot numerically compare numbers. Second, FINDSTR cannot return a substring from a line.

It can be solved using FOR /F to parse the space delimited list of values. Note that this solution is not based on character position.

EDIT: Major rewrite to handle possibility of number larger than 2147483647. Also, my original code failed if it had zeros in the middle of the number

This solution assumes all lines have 10 digits in the 7th token (position 41). I enclose the values in quotes within the IF comparison to force it to do a text comparison instead of a numeric comparison. This will work properly as long as all values have exactly 10 digits.

set "num=0000000000%expirydate%"
set "num=%num:~-10%"
for /f "usebackq tokens=1,7" %%A in ("test.txt") do (
  if "%%C" gtr "0000000001" if "%%C" lss "%expirydate%" (echo %%A )
)

Upvotes: 2

Kokkie
Kokkie

Reputation: 556

If I look at your example and take character 41 to 50 from the first line it gives me: "141508179", but that string is ten characters long; "1415081796" and it doesn't look like a date.

Here you have a VBScript which extracts numbers bigger than 200000000 and places them in a new file.

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Temp\input.txt", ForReading)   
Do While objFile.AtEndOfStream <> True
    strLine = objFile.ReadLine
    extractednr = Mid(strLine,41,9)
    If (extractednr > 200000000) Then
        strContents = strContents & extractednr & vbCrLf
    End If
Loop
objFile.Close

Set objFile = objFSO.CreateTextFile("C:\Temp\output.txt", true)
objFile.WriteLine strContents
objFile.Close

Upvotes: 0

Related Questions