Searching a text file and sending only numbers gre

2019-09-06 02:20发布

I have a text file that I am searching through that 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

At the moment my specific problem is when searching the file for the 1st and 4th tokens. If the 4th token is a number everything sends fine, but when the fourth token is blank (as on the 5th line in my example) the code simply doesn't work. You can see I want to return three asterisks when it finds a blank in the %%B variable.

NOTE: Thanks to the user, LotPings, I'm using the findstr command to grab only lines beginning with strings containing the substring :S: just to clarify why that's there. (Ie. line 1 in my example wouldn't be grabbed.)

I'm using:

setlocal enabledelayedexpansion

For /F "tokens=1,4" %%A in ('Findstr /b /r /c:"[^ ]*:S:" print.log') do (
set space="%%B"
if !space!=="" echo %%A ^*^*^* >> new.txt
)

exit

1条回答
虎瘦雄心在
2楼-- · 2019-09-06 03:14
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion

>new.txt (
  For /F "tokens=1,4" %%A in ('Findstr /b /r /c:"[^ ]*:S:" print.log') do (
    set "space=%%B"
    if "!space!"=="" (echo %%A ^*^*^*) else (echo %%A %%B)
  )
)

type new.txt
pause
查看更多
登录 后发表回答