Remove all lines which do not contain a period

假装没事ソ 提交于 2020-01-06 05:25:54

问题


I have a text like this in %userprofile%\i.txt:

PDF

wikkipedia.ord
notavalidURL
snapfish.com
.tunnelbear.com
mail.google.com/mail/u/0/#inbox

I want to convert it to a string like this:

wikkipedia.ord snapfish.com .tunnelbear.com mail.google.com/mail/u/0/#inbox!

I want to do this using native cmd.exe syntax only (no powershell, cygwin, etc.).

So I know that I can do this:

    @echo off
    setlocal EnableDelayedExpansion
    set row=
    for /f %%x in (%userprofile%\i.txt) do set "row=!row!%%x"
    echo %row% >%userprofile%\o.txt
    echo %row%

How can my result ignore all substrings which do not contain at least one period character?

this will delete the contents of the file:

@echo off
setlocal EnableDelayedExpansion
set row=
@For /F "EOL=|Tokens=*" %%x In ('^""%__AppDir__%find.exe" "."^<"%userprofile%\i.txt"^"') Do @Set "row=!row!%%x"
echo %row% >%userprofile%\o.txt
echo %row%

where as this will join all lines leaving no whitespaces between the sub-strings:

@echo off
setlocal EnableDelayedExpansion
set row=
@For /F "EOL=|Tokens=*" %%x In ('^""%__AppDir__%find.exe" "."^<"%userprofile%\i.txt"^"') Do @Set "row=!row!%%x"
echo %row% >%userprofile%\o.txt
echo %row%

but i need white-spaces between all those substring being put out.

@echo off
setlocal EnableDelayedExpansion
set row=                                         
@For /F "EOL=|Tokens=*" %%x In ('^""%__AppDir__%find.exe" "."^<"%userprofile%\i.txt"^"') Do @Set "row=!row! %%x"
echo %row% >%userprofile%\o.txt
echo %row%

this is a lot better but i have white-spaces left over from the strings that were removed.

i need the output to separate the remaining strings but not clutter the file with random white-spaces. this way i will be able to pass " " as a parameter for a StringSplit() or something like that.

this is the difference between 'Tokens=' in the code and 'Tokens=' taken out:


回答1:


I've already provided in the comments sufficient information for you to correct your original methodology complete with the Find command.

However, given your example file content and stated intention, this may be sufficient for your needs:

@((For /F "EOL=|Delims=" %%# In ('^""%__AppDir__%find.exe" "."^<"%UserProfile%\i.txt"^"')Do @Set /P "=%%# "<NUL)&Echo()>"%UserProfile%\o.txt"


来源:https://stackoverflow.com/questions/59567496/remove-all-lines-which-do-not-contain-a-period

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