2012-01-09 74 views
1

這是我的代碼。使用批次顯示文本文件中特定行後面的所有行

@ECHO OFF 
systeminfo > C:\SystemInfo1.txt 

findstr /C:"OS Name:" /C:"OS Version:" /C:"OS Manufacturer:" /C:"Original Install Date:" /C:"System Boot Time:" /C:"System Manufacturer:" /C:"System Model:" /C:"System Type:" /C:"Processor(s):" /C:"System Locale:" /C:"Input Locale:" /C:"Time Zone:" /C:"NetWork Card" C:\SystemInfo1.txt > c:\SystemInfo.txt 

DEL C:\SystemInfo1.txt 

c:\SystemInfo.txt 
EXIT 

這段代碼的作用是隻顯示所選擇的內容我從文本文件「Systeminfo1.txt」需要的,如果你看到你的系統信息文件在顯示網卡的細節結束。但有了這個代碼,我只能得到第一行。

實際系統的系統信息:

Host Name:     BLRPN0W01085 
OS Name:     Microsoft Windows XP Professional 
OS Version:    5.1.2600 Service Pack 3 Build 2600 
OS Manufacturer:   Microsoft Corporation 
OS Configuration:   Member Workstation 
OS Build Type:    Uniprocessor Free 
Registered Owner:   Admin 
Registered Organization: Thomson Reuters 
Original Install Date:  8/31/2011, 10:46:03 AM 
System Up Time:   0 Days, 10 Hours, 19 Minutes, 2 Seconds 
System Manufacturer:  Dell Inc. 
System Model:    OptiPlex 755     
System type:    X86-based PC 
Processor(s):    1 Processor(s) Installed. 
         [01]: x86 Family 6 Model 23 Stepping 6 GenuineIntel ~2992 Mhz 
BIOS Version:    DELL - 15 
Windows Directory:   C:\WINDOWS 
System Directory:   C:\WINDOWS\system32 
Boot Device:    \Device\HarddiskVolume1 
System Locale:    en-us;English (United States) 
Input Locale:    en-us;English (United States) 
Time Zone:     (GMT) Casablanca 
Total Physical Memory:  2,014 MB 
Available Physical Memory: 772 MB 
Virtual Memory: Max Size: 2,048 MB 
Virtual Memory: Available: 2,008 MB 
Virtual Memory: In Use: 40 MB 
Page File Location(s):  C:\pagefile.sys 
Domain:     tfcorp.tfn.com 
Logon Server:    \\INKABAN1DIR01 
Hotfix(s):     319 Hotfix(s) Installed. 
         [01]: File 1 
         [02]: File 1 
         [03]: File 1 
         [04]: File 1 
         [05]: File 1 
         [06]: File 1 
         [07]: File 1 
         [08]: File 1 
         [09]: File 1 
         [10]: File 1 
         [11]: File 1 
         [12]: File 1 
         [13]: File 1 
         [14]: File 1 
         [15]: File 1 
         [16]: File 1 
         [17]: File 1 
         [18]: File 1 
         [19]: File 1 
         [20]: File 1 
         [21]: File 1 
         [22]: File 1 
         [23]: File 1 
         [24]: File 1 
         [25]: File 1 
         [26]: File 1 
         [27]: File 1 
         [28]: File 1 
         [29]: File 1 
         [30]: File 1 
         [31]: File 1 
         [32]: File 1 
         [33]: File 1 
         [34]: File 1 
         [35]: File 1 
         [36]: File 1 
         [37]: File 1 
         [38]: File 1 
         [39]: File 1 
         [40]: File 1 
         [41]: File 1 
         [42]: File 1 
         [43]: File 1 
         [44]: File 1 
         [45]: File 1 
         [46]: File 1 
         [47]: File 1 
         [48]: File 1 
         [49]: File 1 
         [50]: File 1 
         [51]: File 1 
         [52]: File 1 
         [53]: File 1 
         [54]: File 1 
         [55]: File 1 
         [56]: File 1 
         [57]: File 1 
         [58]: File 1 
         [59]: File 1 
         [60]: File 1 
         [61]: File 1 
         [62]: File 1 
         [63]: File 1 
         [64]: File 1 
         [65]: File 1 
         [66]: File 1 


NetWork Card(s):   1 NIC(s) Installed. 
         [01]: Intel(R) 82566DM-2 Gigabit Network Connection 
           Connection Name: Local Area Connection 
           DHCP Enabled: Yes 
           DHCP Server:  10.136.193.51 
           IP address(es) 
           [01]: 10.136.208.125 

什麼我的代碼顯示:

OS Name:     Microsoft Windows XP Professional 
OS Version:    5.1.2600 Service Pack 3 Build 2600 
OS Manufacturer:   Microsoft Corporation 
Original Install Date:  8/31/2011, 10:46:03 AM 
System Manufacturer:  Dell Inc. 
System Model:    OptiPlex 755     
Processor(s):    1 Processor(s) Installed. 
BIOS Version:    DELL - 15 
System Locale:    en-us;English (United States) 
Input Locale:    en-us;English (United States) 
Time Zone:     (GMT) Casablanca 
NetWork Card(s):   1 NIC(s) Installed. 

缺少什麼:

[01]: Intel(R) 82566DM-2 Gigabit Network Connection 
           Connection Name: Local Area Connection 
           DHCP Enabled: Yes 
           DHCP Server:  10.136.193.51 
           IP address(es) 
           [01]: 10.136.208.125 

我如何獲得缺少什麼?順便說一句,有誰知道爲什麼BIOS顯示爲'DELL - 15'?這只是與XP。

+2

您可能希望下次在發佈之前清理數據。你只是給了你的外部域名,以及缺少100多個修補程序的個人電腦的內部IP。 – brandong 2012-01-10 17:22:52

回答

1

由於您需要根據上下文(即第一張網卡後面的縮進行)選擇行,因此您只需使用「findstr」即可完成所需操作。使用perl,awk,ruby或其他腳本語言來做它是微不足道的。

思考一種方法來做你想要的東西只有Windows的東西,我想到了Javascript。請注意,JavaScript不是我的優勢,這可能會做得更好,但假設你想在「NetWork Cards」之後的所有行,這應該工作:

把它放在一個文件(例如:filter.js) :

function processTextStream() { 
    finder = new RegExp("^((OS (Name|Version|Manufacturer))" + 
        "|(Original Install)|(System (Manuf|Model|Locale))" + 
        "|Processor|BIOS|Input Locale|Time Zone" + 
        ")") 
    netcard = new RegExp("^NetWork Card"); 

    printall = false; 

    while (!WScript.StdIn.AtEndOfStream) { 
     line = WScript.StdIn.ReadLine(); 
     if (finder.test(line)) 
     { 
      WScript.echo(line); 
     } 
     else if (netcard.test(line)) 
     { 
      WScript.echo(line); 
      printall = true; 
     } 
     else if (printall) 
     { 
      WScript.echo(line); 
     } 

    } 
} 

// Main script flow 
processTextStream(); 

的從你的批處理文件,這樣做:

systeminfo | cscript //nologo filter.js >SystemInfo.txt 
0

字符串匹配限制爲單行(即回車之間有什麼)。因此您需要將缺少的標識符添加到搜索字符串列表中。

systeminfo | findstr /C:"NetWork Card" /C:"[01]:" /C:"Connection Name:" /C:"DHCP Enabled:" /C:"DHCP Server:" 

你會注意到這會在你的處理器上帶來額外的信息,因爲[01]也在那裏。如果你認爲一個盒子可能有多個nic,你也可以搜索[02]。

如果你只想在它存在的時候搜索它,那麼你將需要使用第一次運行的systeminfo輸出作爲另一個搜索文件的輸入。例如第一次運行輸出會告訴你1 NIC(s) Installed2 NIC(s) Installed然後,你需要用for循環和字符匹配來解析,以獲得安裝的確切數量的nics,然後再使用它搜索systeminfo。

+0

我不認爲這就是他想要的。他要求在文本文件中提取「在特定行之後」的所有行。 – theglauber 2012-01-10 17:28:04

+0

@theglauber他問:「我如何得到缺少的東西?」這是一個有效的方式,同時保持對findstr的使用。你的方式是使用其他技術的另一種有效方式。 – brandong 2012-01-10 18:13:27

2

這裏,我們去:

@ECHO OFF 
systeminfo >c:\SystemInfo1.txt 

findstr /C:"OS Name:" /C:"OS Version:" /C:"OS Manufacturer:" /C:"Original Install Date:" /C:"System Boot Time:" /C:"System Manufacturer:" /C:"System Model:" /C:"System Type:" /C:"Processor(s):" /C:"System Locale:" /C:"Input Locale:" /C:"Time Zone:" /C:"NetWork Card" c:\SystemInfo1.txt >c:\SystemInfo.txt 

for /f "tokens=1 delims=:" %%a in ('findstr /n "NetWork Card" c:\SystemInfo1.txt') do set line=%%a & goto remainder 

:remainder 
more +%line% c:\SystemInfo1.txt >>c:\SystemInfo.txt 

DEL C:\SystemInfo1.txt 
c:\SystemInfo.txt 
EXIT /b 

如果您肯定知道,只有永遠將是一個NIC(網卡),那麼你可以從的末尾刪除「&轉到其餘」 'FOR-statement'並刪除標籤':其餘'。

相關問題