我有一个文本文件,其中包含我需要提取和导出到电子表格的信息

时间:2014-12-28 00:38:56

标签: batch-file powershell-v2.0

我的NetBackup日程表文本文件包含需要提取和分组的数据字段。

以下示例包含两个策略:

POLICIES

Policy Name:    Windows_Server_1

Policy Type:    MS-Windows-NT
Active:         no
Effective Date:    06/24/2014 01:00:00
Backup network drives:    no
Collect TIR info:    no
Mult. Data Streams:    no
Client Enctypt:    no
Checkpoint:    no
Policy Priority:    0
Max Jobs/Policy:     Unlimited
Disastery Recovery:    0
Collect BMR info:    no
Residence:    BackupSrv01-hcart2
Volume Pool:    NetBackup
Server group:    *Any*
Keyword:    (none specified)
Residence is Lifecycle Policy:    no

Granular Restore Info:    no
HW/OS/Client: PC    Windows2008    WinServer01

Include:    All_Local_Drives

Schedule:     Full
Type:    Full Backup
Frequency:    every 7 days
Maximum MPX:    1
Synthetic:        0
PFI Recovery:     0
Retention Level:    8
Number Copies:    0
Fail on error:    0
Residence:    (specific storage unit not required)
Volume Pool:    (same as policy volume pool)
Server Group:    (Same as specified for policy)
Residence is Storage Lifecycle Policy:    0


Policy Name:    Unix_Server_10

Policy Type:    Unix
Active:         no
Effective Date:    06/24/2014 01:00:00
Backup network drives:    no
Collect TIR info:    no
Mult. Data Streams:    no
Client Enctypt:    no
Checkpoint:    no
Policy Priority:    0
Max Jobs/Policy:     Unlimited
Disastery Recovery:    0
Collect BMR info:    no
Residence:    BackupSrv01-hcart2
Volume Pool:    NetBackup
Server group:    *Any*
Keyword:    (none specified)
Residence is Lifecycle Policy:    no

Granular Restore Info:    no
HW/OS/Client: Unix    Unix    Unix_Server_10 

Include:    All_Local_Drives

Schedule:     Full
Type:    Full Backup
Frequency:    every 7 days
Maximum MPX:    1
Synthetic:        0
PFI Recovery:     0
Retention Level:    8
Number Copies:    0
Fail on error:    0
Residence:    (specific storage unit not required)
Volume Pool:    (same as policy volume pool)
Server Group:    (Same as specified for policy)
Residence is Storage Lifecycle Policy:    0

我想要的每个政策的电子表格输出包括以下字段:

  • 政策名称
  • 政策类型
  • HW
  • OS
  • 客户端
  • 包含
  • 附表

文本文件中可能有50-60个策略。 我正在研究一个powershell - 但是没有在哪里。 :(

越简越好......

新年快乐!

1 个答案:

答案 0 :(得分:0)

@ECHO OFF
SETLOCAL
::
:: General-purpose CSV generator
::
CALL :zap$#
:: Column-names in output order
SET /a outcolumns=0
FOR %%a IN ("Policy Name" "Policy Type" Keyword
            HW OS Client Include Schedule) DO CALL :set# %%a
:: Where does a new data item begin?
SET "newdataitem=Policy Name"
:: Read-and-generate
(
FOR /f "tokens=1*delims=:" %%a IN (q27672783.txt) DO (
 IF "%%a"=="%newdataitem%" CALL :showline
 SET "#0=%%a"
 FOR /f "tokens=*" %%c IN ("%%b") DO SET "newdata=%%c"&CALL :set$ 
)
IF DEFINED $0 CALL :showline
)>"newfile.txt"
GOTO :EOF

:: Increment outcolumn count & record name & record header & set 'header loaded'
:set#
SET /a outcolumns+=1
SET #%outcolumns%=%~1
SET $%outcolumns%=%~1
SET $0=Y
GOTO :eof

:: Store newdata in column #0
:set$
FOR /f "tokens=1*delims==" %%d IN ('set #') DO IF "%%e"=="%#0%" SET selected=%%d
SET "$%selected:~1%=%newdata%"
SET $0=Y
GOTO :eof

:showline
SET "outline="
FOR /L %%g IN (1,1,%outcolumns%) DO (CALL SET "outline=%%outline%%,"%%$%%g%%"")
ECHO %outline:~1%
)
CALL :zap$
GOTO :eof

:: remove variables starting $ or #
:zap$#
For %%b IN ($ #) DO FOR  /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
GOTO :eof

:: remove variables starting $
:ZAP$
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
GOTO :eof

这是用于此类文件的原始CSV生成器。每个提交的作品都会被引用。您提供的数据中没有“硬件”或“操作系统”或“客户端”数据项 - 它们对您而言意味着什么,而不是我 - 您必须明确。我添加了“关键字”列只是为了证明(parenthesised word sequences)被正确处理,但正如我所说的那样,'原始的,可能不会干净地处理许多常见的文本形式,也没有冒险只引用包含列的文件-separators(在这种情况下是逗号)

我使用了一个名为q27672783.txt的文件,其中包含我的测试数据。

制作newfile.txt