简体   繁体   中英

How to select only number in txt file using command line functions in Windows

I have a file that has this format:

<somecharacters> 1 <somecharacters>
<somecharacters> 2 <somecharacters>
<somecharacters> 4 <somecharacters>
<somecharacters> 9 <somecharacters>

I need to parse out the largest numeral. I tried using FINDSTR and FIND , but just don't have enough experience with the Windows command line...

If all the lines in your file are really consistently formatted like that, then this should work (I used the attribute instead of the inner text)

@echo off
setlocal enableDelayedExpansion
set maxPort=0
for /f "tokens=6 delims=<./ " %%N in (test.txt) do (
  if %%N gtr !maxPort! set maxPort=%%N
)
echo maxPort=%maxPort%

But if you have additional lines that look different that should be ignored, you will want to use FINDSTR to filter out the lines you don't want to parse. The FINDSTR command would go inside the IN() clause. FINDSTR has limited support for regular expressions that probably meet your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM