Using FindStr + length in batch

限于喜欢 提交于 2019-12-12 01:46:08

问题


In my text file is this line.

08.01.2013 20:06:36: SolsticeFTW (109.189.238.113:60381) 04ee8dfa81f700bba194a528911ecb4a - #0
08.01.2013 20:11:00: SolsticeFTW (109.189.238.113:60381) 04ee8dfa81f700bba194a528911ecb4a - #0
08.01.2013 20:11:24: SolsticeFTW (109.189.238.113:60381) 04ee8dfa81f700bba194a528911ecb4a - #0
08.01.2013 20:11:50: SolsticeFTW (109.189.238.113:60381) 04ee8dfa81f700bba194a528911ecb4a - #0
09.01.2013 15:39:57: lalka (95.165.55.92:15609) 161c5a17c5afc0b1e079fca7fdc8f1a9 - #0

I want to Filter a text file per the Command Findstr, this works but I cant get it to work that only filters the 32 digit word. For example 04ee8dfa81f700bba194a528911ecb4a and 161c5a17c5afc0b1e079fca7fdc8f1a9. It only works so far by filtering the complete line.

Does anyone know how i am able to filter just the 32 digit symbols/token?


回答1:


FINDSTR can only print the entire line that matches. It does not have the capability of something like grep that can list only the matching portion of the text.

The FOR /F command can often be used to extract tokens from a line of text, depending on the format of the data. You could use FINDSTR to select only the appropriate lines, and FOR /F to process those results and extract the 32 digit token.

I don't know what your FINDSTR search criteria is, so I'll make something up for use as an example. Suppose you want to print out the 32 digit token for lines that end with #0.

FINDSTR selects the appropriate lines, looking for lines that end with #0. And FOR /F parses each resultant line into tokens, delimited by spaces. (The token delimiter can be changed via the DELIMS option, but it is space and tab by default). You want the 5th token.

for /f "tokens=5" %%A in ('findstr /le " #0" "someFile.txt"') do echo %%A


Edit in response to OP comment

If you don't need to filter the lines and you simply want to write each 32 digit ID to a new file, then it is even simpler:

(for /f "tokens=5" %%A in (logged-players.log) do echo %%A) >logged-bans.txt


来源:https://stackoverflow.com/questions/14363981/using-findstr-length-in-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!