How do I apply this?

北战南征 提交于 2019-12-11 19:05:30

问题


I would like to use as the variable for my code below, instead of what comes after ClassName= in 1.txt, I would like what comes in-between this:

EntryText=Ship sunk!|Grid AO 77|Variable,

(notice the comma after the variable and the | before it )

So grab after the text line ending with the second | and before the comma.

The text line before the variable will be the same and constant EXCEPT after "Grid" there could be any mixture of letters and numbers up until the second |

So I am trying to use as a variable, what is in between:

EntryText=Ship sunk!|Grid (Any combination of letters or numbers) | (variable) , (comma)

So grab in between the second | and the comma. Than you. I would like to replace the grabbing of the variable after ClassName= to what is in between the second | and the comma. Please keep in mind that there are other | and commas in the file that I don't want to grab, I just want to grab the variable after | and before comma if its after the "EntryText=Ship sunk!|Grid ....

Again, I don't want the Grid part, I'd like what comes after the second | and before the comma. There will be many EntryText lines as well, so I'd like to grab that part of all of them and put in my code.

So instead of what is after ClassName=, I'd like to copy what is where the variable listed above.

Thank you for your time!!

@echo off

copy 2.txt 2.txt-backup

setlocal enableDelayedExpansion

>2.txt (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" 2.txt-backup') do (
( echo !ln!| findstr "^Type=206$" >NUL && set ln=ln ) || (
    set "ln=%%B"
    if "!ln:~0,6!"=="Class=" (
        findstr /c:"ClassName=!ln:~6!" "E:\Dropbox\New folder\Log_*.txt" >"E:\Dropbox\New folder\null" && (
            echo Class=ShipDummy
            set "ln=Type=206"
        )
    )
    if #!ln!==# (echo;) else echo !ln!
)
)
)

I was given this bottom code by someone, but I don't know if its what I want or how to apply it to the above:

for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do echo %%D

Thank you!!!!!


回答1:


I think you want something like this...

...
set "ln=%%B"
for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do set "MyVar=%%D"
findstr /c:"ClassName=!MyVar!" "E:\Dropbox\New folder\Log_*.txt" >"E:\Dropbox\New folder\null" && (
    echo Class=ShipDummy
    ...


来源:https://stackoverflow.com/questions/15017118/how-do-i-apply-this

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