delim空格和制表符与网页副本

时间:2016-12-02 12:52:28

标签: windows batch-file token

我正在运行从我的工作网页复制到文本文件的文本循环。我发现从谷歌浏览器中复制,文本内容已经过了#34; delims" " TAB"。如果我从Internet Explorer复制相同的信息,Delims是" SPACES",与Firefox相同。然后,这会改变令牌等的数量,并且每种类型需要两组不同的代码。

所以...(实施例)

Chrome

文本复制格式

2 Dec 2016 12:37 GMT    194.176.105.169 United Kingdom  ID007638.CENTRAL    3.10.6.0    Remove
2 Dec 2016 12:36 GMT    194.176.105.152 United Kingdom  ID007578.CENTRAL    3.10.6.0    Remove
2 Dec 2016 12:34 GMT    194.176.105.166 United Kingdom  ID006715.CENTRAL    3.10.6.0    Remove

for /F "usebackq tokens=5 delims=   " %%J in ("hosts.txt") do @echo(%%J

Internet Explorer

文本复制格式

2 Dec 2016 10:16 GMT 194.176.105.132 United Kingdom  ID007643.CENTRAL 3.10.6.0 Remove 
2 Dec 2016 10:16 GMT 194.176.105.133 United Kingdom  ID006967.CENTRAL 3.10.6.0 Remove 
1 Dec 2016 17:59 GMT 194.176.105.139 United Kingdom  ID006972.CENTRAL 3.10.6.0 Remove 
1 Dec 2016 17:55 GMT 194.176.105.132 United Kingdom  ID007574.CENTRAL 3.10.6.0 Remove 

for /F "usebackq tokens=10 delims= " %%J in ("hosts.txt") do @echo(%%J

所以我可以让用户选择你是从Chrome或IE,Forefox复制然后做代码xyz,但我宁愿代码查看文本文件,不知何故看到它使用空格作为delims然后运行代码对于空格和delim,或者如果它找到tab delims然后运行代码。这是我为同事做的一个小项目,他们做得越少越好。这可能吗?也许在空格(?)上运行一个find命令,然后执行X或找到TABS的存在然后执行y。

2 个答案:

答案 0 :(得分:1)

我认为这种方法可以解决您的问题:

@echo off
setlocal EnableDelayedExpansion

for /F "usebackq delims=" %%a in ("hosts.txt") do (
   for %%b in (%%a) do (
      set "hostname=!version!"
      set "version=!last!"
      set "last=%%b"
   )
   echo Hostname: !hostname!,  version: !version!
)

此方法采用空格或TAB分隔的行的最后一个和“最后两个”(正确写入?)字段。

编辑作为回复评论的新解决方案

@echo off
setlocal EnableDelayedExpansion

rem Define the fields
set "field[1]=Version"
set "field[2]=Hostname"
set "field[4]=IPaddress"

rem Show the options
:start
cls
set "choice="
for /L %%i in (1,1,7) do (
   set "option=%%i. Extract"
   for %%j in (1,2,4) do (
      set /A "j=%%i&%%j"
      if !j! neq 0 set "option=!option! !field[%%j]! and"
   )
   echo !option:~0,-4!
   set "choice=!choice!%%i"
)
echo X. Exit
echo/

rem Get the option and assemble the output format
choice /C %choice%X /N /M "Select option: "
set "choice=%errorlevel%"
if %choice% gtr 7 goto :EOF
set "output="
set "file="
for %%j in (1,2,4) do (
   set /A "j=choice&%%j"
   if !j! neq 0 (
      set "output=!output!^!!field[%%j]!^!  "
      set "file=!file!!field[%%j]! "
   )
)

rem Read input file and generate output file
(for /F "usebackq tokens=6*" %%a in ("hosts.txt") do (
   set "IPaddress=%%a"
   for %%c in (%%b) do (
      for %%d in ("!version!") do set "hostname=%%~Nd"
      set "version=!last!"
      set "last=%%c"
   )
   echo %output%
)) > "%file:~0,-1%.txt"
start "" "%file:~0,-1%.txt"
goto start

答案 1 :(得分:0)

此代码的工作方式与您抛出的任何格式(复制空格或制表符)一样,它会创建一个间隔分隔的文本文件,其中包含所有提取的信息,然后进一步询问..

从任何浏览器复制并粘贴到hosts.txt然后将内容输出到另一个文本文件....(hosts2.txt)

for /F "usebackq delims=" %%a in ("hosts.txt") do (
   for %%b in (%%a) do (
      set "hostname=!version!"
      set "version=!last!"
      set "last=%%b"
    )
    echo !hostname! !version!>>"hosts2.txt"
)

然后我使用以下内容显示"只显示txt文件中的主机名","只显示txt文件中的版本号"或者在txt文件中使用"版本和主机名"

:start
CLS
echo.
echo                    :::::::::::::::::::::::::::::::::::::::::::::::::
echo                    ::        1. Extract Hostnames                 ::
Echo                    ::        2. Extract Version                   ::
echo                    ::        3. Extract Hostnames and Version     ::
echo                    :::::::::::::::::::::::::::::::::::::::::::::::::
echo.
SET /P "Input= "
if '%Input%'=='1' (goto run1)
if '%Input%'=='2' (goto run2)
if '%Input%'=='3' (goto run3)

:run1
> "hostnames.txt" (
        for /F "usebackq tokens=1 delims=." %%J in ("hosts2.txt") do @echo(%%J
)
start "" "hostnames.txt"
goto start

:run2
> "version.txt" (
    for /F "usebackq tokens=2 delims= " %%J in ("hosts2.txt") do @echo(%%J
)
start "" "version.txt"
goto start


:run3
> "hostnver.txt" (
    for /F "usebackq tokens=1,2 delims= " %%J in ("hosts2.txt") do @(
        for /F "tokens=1 delims=." %%I in ("%%J") do @echo(%%I %%K
    )
)
start "" "hostnver.txt"
goto start