Display all the lines after a particular line in a text file using Batch

爱⌒轻易说出口 提交于 2019-12-11 11:59:52

问题


This is my code.

@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

What this code does is displays only the selected content i need from the text file 'Systeminfo1.txt' if you see your system info file at the end the Network Card details are displayed. But with this code i get only the first line.

Actual SystemInfo:

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

What my code displays:

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.

What is missing:

[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

How do i get What is missing? BTW does anyone know why BIOS is shown as 'DELL - 15' ?? this is only with XP.


回答1:


Here we go:

@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

If you know for sure there is only ever going to be ONE NIC (network card) then you can remove the '& goto remainder' from the end of the 'FOR-statement' and remove the label ':remainder'.




回答2:


It's not possible to do what you want using only "findstr", because you need to select lines based on their context (i.e. The indented lines that follow the first network card). It's trivial to do it using perl, awk, ruby or other scripting languages.

Thinking of a way to do what you want with Windows stuff only, i thought of Javascript. Be aware that Javascript is not my forte, and this can probably be made better, but assuming you want all the lines after "NetWork Cards", this should work:

put this in a file (e.g.: 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();

The from your batch file, do:

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



回答3:


string match is limited to a single line (ie what's between carriage returns). so you will need to add the missing identifiers to your list of search strings.

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

you'll notice this will bring in extra info on your processor too because [01] matches there too. if you think a box might have more than one nic you could search for [02] also.

if you only want to search this when it's there then you will need to use your first run of systeminfo output as input for another search file. eg, first run output will tell you 1 NIC(s) Installed or 2 NIC(s) Installed You will then need to parse that with a for loop and character matching to get the exact number of nics installed then search systeminfo again using that.



来源:https://stackoverflow.com/questions/8795051/display-all-the-lines-after-a-particular-line-in-a-text-file-using-batch

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