1

我需要操作一个文本文件,我使用 wmic 脚本创建了它。

原始文件:

Node,Manufacturer,Model
NAKAMA,Dell Computer Corporation,PowerEdge 6650             
Node,Name,Version
NAKAMA,Microsoft Windows Server 2003 R2 Enterprise Edition|C:\WINDOWS|\Device\Harddisk0\Partition1,5.2.3790


Node,AddressWidth
NAKAMA,32
NAKAMA,32
NAKAMA,32
NAKAMA,32


Node,SerialNumber
NAKAMA,BZJS571


Node,Name,Version
NAKAMA,Microsoft Office Excel Viewer,12.0.6219.1000
NAKAMA,Microsoft Software Update for Web Folders  (English) 14,14.0.4763.1000
NAKAMA,Microsoft Office Visio MUI (English) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Shared Setup Metadata MUI (English) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Visio 2010,14.0.4763.1000
NAKAMA,Microsoft Office Proofing (English) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Shared MUI (English) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Proof (English) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Proof (Spanish) 2010,14.0.4763.1000
NAKAMA,Microsoft Office Proof (French) 2010,14.0.4763.1000
NAKAMA,MSXML 6 Service Pack 2 (KB954459),6.20.1099.0
NAKAMA,Microsoft SQL Server 2005 Express Edition (DTC),9.2.3042.00
NAKAMA,Microsoft .NET Framework 3.0 Service Pack 2,3.2.30729
NAKAMA,Microsoft Group Policy Management Console with SP1,1.0.2.0
NAKAMA,Microsoft Windows SDK for Windows 7 (7.1),7.1.30514
NAKAMA,Microsoft SQL Server Native Client,9.00.3042.00
NAKAMA,Broadcom Management Programs,10.38.04
NAKAMA,Microsoft .NET Framework 3.5 SP1,3.5.30729
NAKAMA,Symantec Endpoint Protection,11.0.7101.1056
NAKAMA,Windows Resource Kit Tools - ClusterRecovery.exe,1.0.0.1
NAKAMA,Java(TM) 6 Update 11,6.0.110
NAKAMA,Windows Resource Kit Tools,5.2.3790
NAKAMA,ILMT-TAD4D Agent,7.2.2.1
NAKAMA,Microsoft Active Directory Topology Diagrammer,2.2.4146
NAKAMA,IBM Tivoli Storage Manager Client,05.04.0100
NAKAMA,TivReDist,1.00.0000
NAKAMA,Adobe Reader X (10.1.0),10.1.0
NAKAMA,ActivePerl 5.10.0 Build 1004,5.10.1004
NAKAMA,Active Directory Migration Tool,2.0.0.0
NAKAMA,Microsoft Easy Assist v2,8.1.6416.0
NAKAMA,Microsoft SQL Server 2005 Express Edition (SQLEXPRESS),9.2.3042.00
NAKAMA,Dell OpenManage Server Administrator,5.5.0
NAKAMA,Windows Resource Kit Tools - GPInventory.exe,1.0.1361.27800
NAKAMA,Microsoft SQL Server VSS Writer,9.00.3042.00
NAKAMA,VMware Remote Console Plug-in,2.5.0.199067
NAKAMA,Microsoft SQL Server 2005 Tools Express Edition,9.2.3042.00
NAKAMA,Microsoft Visual C++ 2005 Redistributable,8.0.56336
NAKAMA,Microsoft Visual J# 2.0 Redistributable Package - SE,2.0.50728
NAKAMA,VMware vSphere Client 5.1,5.1.0.1557
NAKAMA,Windows Server 2003 Service Pack 2 Administration Tools Pack,5.2.3790.3959
NAKAMA,Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4148,9.0.30729.4148
NAKAMA,Microsoft Visual C++ 2010  x86 Redistributable - 10.0.30319,10.0.30319
NAKAMA,Microsoft .NET Framework 2.0 Service Pack 2,2.2.30729
NAKAMA,Windows Support Tools,5.2.3790.3959
NAKAMA,Quest ActiveRoles Management Shell for Active Directory,1.5.1.2421
NAKAMA,MSXML 4.0 SP2 (KB954430),4.20.9870.0
NAKAMA,Debugging Tools for Windows (x86),6.12.2.633
NAKAMA,Microsoft SQL Server Setup Support Files (English),9.00.3042.00
NAKAMA,CBMR 5.0.1 Release 4,5.0.1

期望的输出:

Node,Manufacturer,Model,Name,Version,AddressWidth,SerialNumber,Name,Version
NAKAMA,Dell Computer Corporation,PowerEdge 6650,Microsoft Windows Server 2003 R2 Enterprise Edition|C:\WINDOWS|\Device\Harddisk0\Partition1,5.2.3790,32,BZJS571,CBMR 5.0.1 Release 4,5.0.1

原始文件是分开的,我只需要它的一部分。每次我查询信息时,我都会获得关于我正在查询的内容的描述以及它下面的结果。

但是现在我需要以这种方式组织数据:

第一:节点,制造商,型号:我需要所有这些

第二:Node,Manufacturer,Model:我只需要Manufacturer,Model(我会从剩下的行中排除Node)

第三:Node,AddressWidth:我只需要AddressWidth(一次就够了)

第四:Node,SerialNumber :我只需要 SerialNumber

第五:节点,名称,版本:我只需要名称,版本,但只需要包含 CBMR 的行

我不确定这是否可能。

4

1 回答 1

0

这是使用awk. 像这样运行:

awk -f script.awk file

内容script.awk

BEGIN {
    FS=OFS=","
}

$1 == x=("Node") {
    c++

    f=sub(/[^,]*,/,"")
    one = (one ? one : x) OFS $0
    next
}

c==5 && $2 !~ /CBMR/ {
    next
}

f==1 {
    y=$1

    gsub(/^[^,]*,|[ \t]*$/,"")
    two = (two ? two : y) OFS $0
    f=0
}

END {
    print one ORS two
}

结果:

Node,Manufacturer,Model,Name,Version,AddressWidth,SerialNumber,Name,Version
NAKAMA,Dell Computer Corporation,PowerEdge 6650,Microsoft Windows Server 2003 R2 Enterprise Edition|C:\WINDOWS|\Device\Harddisk0\Partition1,5.2.3790,32,BZJS571,CBMR 5.0.1 Release 4,5.0.1

或者,这是单线:

awk 'BEGIN { FS=OFS="," } $1 == x=("Node") { c++; f=sub(/[^,]*,/,""); one = (one ? one : x) OFS $0; next } c==5 && $2 !~ /CBMR/ { next } f==1 { y=$1; gsub(/^[^,]*,|[ \t]*$/,""); two = (two ? two : y) OFS $0; f=0 } END { print one ORS two }' file
于 2013-01-05T00:15:16.117 回答