Board logo

标题: 如果将文本里的指定信息提取出来? [打印本页]

作者: worldopener     时间: 2008-4-29 12:33    标题: 如果将文本里的指定信息提取出来?

用命令ipconfig /all >>ip.txt 可以将IP信息输出到文件,如:
Windows IP Configuration

        Host Name . . . . . . . . . . . . : owq

        Primary Dns Suffix  . . . . . . . :

        Node Type . . . . . . . . . . . . : Unknown

        IP Routing Enabled. . . . . . . . : No

        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :

        Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit Ethernet NIC

        Physical Address. . . . . . . . . : 00-1B-FC-C0-2B-86

        Dhcp Enabled. . . . . . . . . . . : No

        IP Address. . . . . . . . . . . . : 192.168.2.234

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.2.200

        DNS Servers . . . . . . . . . . . : 202.96.134.133


我只想把里面的用户(owq)、IP(192.168.2.234)和mac地址(00-1B-FC-C0-2B-86)提取出来,输出到另外一个文本ipinfo.txt里,并按照如下的方式排列(中间用Tab键或空格分隔):

owq   192.168.2.233   00-1B-FC-C0-2B-86

请各位高手指教一下,不胜感激!!

[ Last edited by worldopener on 2008-4-29 at 12:35 PM ]
作者: abcd     时间: 2008-4-29 12:47

@echo off
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|findstr /i "host address"') do (
  call,set str=%%str%%%%i
)
echo %str%
pause

作者: worldopener     时间: 2008-4-29 13:07
非常感谢abcd的指导,受益匪浅啊!
如果我有很多个这样的文本,如:
A.txt

Windows IP Configuration

        Host Name . . . . . . . . . . . . :S1
        Primary Dns Suffix  . . . . . . . :

        Node Type . . . . . . . . . . . . : Unknown

        IP Routing Enabled. . . . . . . . : No

        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :

        Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit Ethernet NIC

        Physical Address. . . . . . . . . : 00-1B-FC-C0-2B-86

        Dhcp Enabled. . . . . . . . . . . : No

        IP Address. . . . . . . . . . . . : 192.168.2.234

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.2.200

        DNS Servers . . . . . . . . . . . : 202.96.134.133


B.txt

Windows IP Configuration

        Host Name . . . . . . . . . . . . :S2

        Primary Dns Suffix  . . . . . . . :

        Node Type . . . . . . . . . . . . : Unknown

        IP Routing Enabled. . . . . . . . : No

        WINS Proxy Enabled. . . . . . . . : No

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . :

        Description . . . . . . . . . . . : Realtek RTL8169/8110 Family Gigabit Ethernet NIC

        Physical Address. . . . . . . . . : 00-1B-FC-C0-2C-96

        Dhcp Enabled. . . . . . . . . . . : No

        IP Address. . . . . . . . . . . . : 192.168.2.235

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        Default Gateway . . . . . . . . . : 192.168.2.200

        DNS Servers . . . . . . . . . . . : 202.96.134.133


想把每个文本里的用户、IP和mac地址一一提取出来,输出到另外一个文本ipinfo.txt里,格式如下:
S1   192.168.2.234   00-1B-FC-C0-2B-86
S2   192.168.2.235   00-1B-FC-C0-2C-96


那又应该如何写代码呢?

[ Last edited by worldopener on 2008-4-30 at 08:41 AM ]
作者: terrytong     时间: 2008-4-29 15:59
abcd 介意详细解释一下for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|findstr /i "host address"') do (
  call,set str=%%str%%%%i
)的意思。谢谢...
作者: abcd     时间: 2008-4-29 16:20
就是从ipconfig的结果中find包含host和address的行,然后通过for截取冒号(:)后面的字符。

再把截取到的字符连接起来。
作者: jfm034     时间: 2008-4-29 16:58
正想着如何实现呢。。没想到已经有现成的

谢谢。。

是否能自动叠加在同一文本内。。??
作者: abcd     时间: 2008-4-29 17:03
echo .......>>test.txt
作者: terrytong     时间: 2008-4-29 18:48
for /f "delims=:" %%i  应该是截取冒号(:)前面的字符啊,为什么会能截取后面的啊?
作者: abcd     时间: 2008-4-29 19:14
tokens=2
作者: xvzheng     时间: 2008-4-29 23:41
你实在是太好了。我喜欢这样的批处理
作者: worldopener     时间: 2008-4-30 08:43
如果是从多个文本里提取信息应怎么写代码呢?
作者: jfm034     时间: 2008-4-30 10:38
修改下。。。:

@echo off
for /f "tokens=2 delims=:" %%i in ('findstr /i "host description address gateway dns" ip.txt') do (
call,set str=%%str%%%%i
)
echo %str%>>test.txt

pause

本人刚学这个。。只能修改为从特定的文件中取相应的信息。。
还不会用循环处理。。

有待高手提示。。。
作者: 26933062     时间: 2008-4-30 10:56
:
@echo off&setlocal enabledelayedexpansion
for /f "tokens=3 delims=:" %%i in ('findstr /i "host address" *.txt') do (
   set /a m+=1
   set /p=%%i<nul
   if !m! equ 3 set m=0&echo.
)
pause

作者: terrytong     时间: 2008-4-30 11:15
for /f "tokens=2" %%i in ('ipconfig /all^|findstr /i "host address"') 会显示 NameAddress.Adress.
for /f "delims=:" %%i in ('ipconfig /all^|findstr /i "host address"')会显示
Host Name . . . . . . . . . . . .         Physical Address. . . . . . . . .         IP Address. . . . . . . . . . . .
for /f "tokens=2 delims=:" %%i in ('ipconfig /all^|findstr /i "host address"')怎么能显示owq   192.168.2.233   00-1B-FC-C0-2B-86 呢?
还是不太懂啊?
作者: worldopener     时间: 2008-4-30 12:09


  Quote:
Originally posted by abcd at 2008-4-30 10:51 AM:

多个文本的话,再在for外层加个for来读取文件名,再把文件名传递给内层for

[ Last edited by abcd on 2008-4-30 at 10:53 AM ]

请问怎么加外层for循环呢?
作者: worldopener     时间: 2008-4-30 12:22


  Quote:
Originally posted by abcd at 2008-4-30 12:13 PM:
可以用14楼的方法啊,

不过个人不大喜欢变量延迟而已.

14楼的方法并不能得到想要的结果,而且没有注解,看不懂!
有没更好的方法呢?
作者: worldopener     时间: 2008-4-30 12:43


  Quote:
Originally posted by abcd at 2008-4-30 12:32 PM:
14楼的方法是可以的.



就是通过findstr查找当前目录下的txt文件中所需的信息.
因为一个文本中有三条信息,所以用一个累加数来计算.当m变量等于3时 ...

但是运行之后,得到要结果是:
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .

而且不能输出到文本里

[ Last edited by worldopener on 2008-4-30 at 12:51 PM ]
作者: worldopener     时间: 2008-4-30 12:53
不是,结果只显示这些信息
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .
Host Name . . . . . .   Physical Address. . . . .    IP Address. . . . . . . .
并没有把用户、Mac地址和IP的实际数据显示出来

[ Last edited by worldopener on 2008-4-30 at 12:55 PM ]
作者: worldopener     时间: 2008-4-30 12:54
如果把代码里的"*.txt"替换成实际的文件名(如:A.txt)的话,就可以正确地显示想要的信息出来
作者: 26933062     时间: 2008-4-30 13:19
你确定是复制14楼的代码吗、?
2楼的是 tokens=2        14的是 tokens=3
作者: worldopener     时间: 2008-4-30 13:24
是的,我复制的代码是tokens=3
作者: worldopener     时间: 2008-4-30 13:26
我的那些文本文件是中文名的,不知有没有关系呢?
作者: 26933062     时间: 2008-4-30 13:28
运行  findstr /i "host address" *.txt  看看显示什么?贴一部分上来。
作者: worldopener     时间: 2008-4-30 13:52


  Quote:
Originally posted by worldopener at 2008-4-30 01:26 PM:
我的那些文本文件是中文名的,不知有没有关系呢?

A.txt:        Host Name . . . . . . . . . . . . : S12

A.txt:        Physical Address. . . . . . . . . : 00-04-61-95-93-2E

A.txt:        IP Address. . . . . . . . . . . . : 192.168.0.102

B.txt:        Host Name . . . . . . . . . . . . : S10

B.txt:        Physical Address. . . . . . . . . : 00-04-61-95-93-2E

B.txt:        IP Address. . . . . . . . . . . . : 192.168.0.100

C.txt:        Host Name . . . . . . . . . . . . : S11

C.txt:        Physical Address. . . . . . . . . : 00-04-61-95-93-2E

C.txt:        IP Address. . . . . . . . . . . . : 192.168.0.101

我现在把那些文件改成了英文名了
作者: worldopener     时间: 2008-4-30 13:57
我也很奇怪,照你的思路是应该可以实现的啊,但不知道为什么显示不出想要的信息来。
作者: suntb     时间: 2008-4-30 22:20


  Quote:
Originally posted by terrytong at 2008-4-29 18:48:
for /f "delims=:" %%i  应该是截取冒号(:)前面的字符啊,为什么会能截取后面的啊?

在"delims=:"中
冒号(:)仅作为分隔符 自然也能截取后面的数据

比如说文本test.txt内容为
AA:BB:CC

for /f "tokens=1-3 delims=:" %%i in (test.txt) do echo %%i %%j %%k
显示为
AA BB CC

[ Last edited by suntb on 2008-4-30 at 10:25 PM ]