none
Transformar TXT em CSV RRS feed

  • Pergunta

  • <object height="0" id="9839c929-b978-849d-18b8-4bb999b1918a" type="application/gas-events-abn" width="0"></object>

    Amigos,

    Preciso tratar um arquivo de log, transformando-o em um CSV. 

    As informações são dispostas linha à linha (item / resultado) e a intenção é organizar em uma matriz, filtrando apenas os itens que interessam.

    Este é um exemplo do log inicial:

    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] BrightStor ARCserve Backup -- Backup
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] 
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Job No....................... 16
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Job ID....................... 564
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Description.................. GRP_2_Weekly_FULL
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Workstation.................. SERVER01
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Source....................... C:
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Target....................... W1_1_GRP2, ID CDE6, Sequence #1
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Session...................... 1
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Start Time................... 7/19/13  7:01 PM
    [07/19/2013-19:01:59 ,0,0,0,0,0,2,0,0,0] Session Method............... Full (Clear Archive Bit)
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Total Directories............ 9,581
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Total File(s)................ 79,516
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Total Skip(s)................ 0
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Total Size (Disk)............ 15,427.62 MB
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Total Size (Media)........... 15,680.50 MB
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Elapsed Time................. teste 28m 18s
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Average Throughput........... 554.08 MB/min
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Session Status............... Finished
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] 
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Job No....................... 16
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Job ID....................... 564
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Workstation.................. SERVER01
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Source....................... F:
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Target....................... W1_1_GRP2, ID CDE6, Sequence #1
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Session...................... 2
    [07/19/2013-19:30:19 ,0,0,0,0,0,2,0,0,0] Start Time................... 7/19/13  7:30 PM
    [07/19/2013-19:30:20 ,0,0,0,0,0,2,0,0,0] Session Method............... Full (Clear Archive Bit)
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Total Directories............ 7,303
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Total File(s)................ 118,224
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Total Skip(s)................ 0
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Total Size (Disk)............ 89,746.15 MB
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Total Size (Media)........... 90,112.37 MB
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Elapsed Time................. 34m 13s
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Average Throughput........... 2,633.55 MB/min
    [07/19/2013-20:04:35 ,0,0,0,0,0,2,0,0,0] Session Status............... Finished
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] 
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Job No....................... 16
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Job ID....................... 564
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Workstation.................. SERVER01
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Source....................... H:
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Target....................... W1_1_GRP2, ID CDE6, Sequence #1
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Session...................... 3
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Start Time................... 7/19/13  8:04 PM
    [07/19/2013-20:04:36 ,0,0,0,0,0,2,0,0,0] Session Method............... Full (Clear Archive Bit)
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Total Directories............ 1
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Total File(s)................ 8
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Total Skip(s)................ 0
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Total Size (Disk)............ 9,955.96 MB
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Total Size (Media)........... 9,956.00 MB
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Elapsed Time................. 2m 55s
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Average Throughput........... 3,413.48 MB/min
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Session Status............... Finished
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] 
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Job No....................... 16
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Job ID....................... 564
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Workstation.................. SERVER01
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Source....................... System State
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Target....................... W1_1_GRP2, ID CDE6, Sequence #1
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Session...................... 4
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Start Time................... 7/19/13  8:07 PM
    [07/19/2013-20:07:32 ,0,0,0,0,0,2,0,0,0] Session Method............... Full (Clear Archive Bit)
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Directories............ 179
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total File(s)................ 3,168
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Skip(s)................ 0
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Size (Disk)............ 571.38 MB
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Size (Media)........... 581.12 MB
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Elapsed Time................. 1m 49s
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Average Throughput........... 319.88 MB/min
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Session Status............... Finished
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] 
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Totals For................... SERVER01
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Session(s)............. 4
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Directories............ 17,064
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total File(s)................ 200,916
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Skip(s)................ 0
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Size (Disk)............ 115,701.13 MB
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Total Size (Media)........... 116,330.00 MB
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Elapsed Time................. 1h 7m 15s
    [07/19/2013-20:09:23 ,0,0,0,0,0,2,0,0,0] Average Throughput........... 1,729.80 MB/min

    Este é um exemplo do resultado esperado (sem a necessidade da formatação, podendo ser um CSV):

    Agradeço antecipadamente


    TulioCass

    terça-feira, 20 de agosto de 2013 12:01

Respostas

  • É dificil mas não impossivel.

    Sabe ler linha por linha do arquivo?

    Veja um começo, é muito trabalhoso então eu não consigo fazer pra você.

    $arquivo = Get-Content C:\temp\technet.txt
    
    foreach ($linha in $arquivo){
    
        #"Lendo $linha"
    
        $linha.contains("Workstation.................")
    
    switch ($true) 
        {
            #Workstation
            $linha.contains("Workstation.................") {
                           
                $temp = $Linha.split(" ")
    
                $servidor=$temp[-1]
                $servidor
            } 
    
            #Source
            ($linha.contains("Source.......................")) {
                "faça algo quando achar Source _ $servidor"
                
                $temp = $Linha.split(" ")
    
                $source=$temp[-1]
                "$servidor $source"
            } 
    
            
        }
    
    }



    Fábio de Paula Junior

    quarta-feira, 21 de agosto de 2013 14:18
    Moderador

Todas as Respostas

  • É dificil mas não impossivel.

    Sabe ler linha por linha do arquivo?

    Veja um começo, é muito trabalhoso então eu não consigo fazer pra você.

    $arquivo = Get-Content C:\temp\technet.txt
    
    foreach ($linha in $arquivo){
    
        #"Lendo $linha"
    
        $linha.contains("Workstation.................")
    
    switch ($true) 
        {
            #Workstation
            $linha.contains("Workstation.................") {
                           
                $temp = $Linha.split(" ")
    
                $servidor=$temp[-1]
                $servidor
            } 
    
            #Source
            ($linha.contains("Source.......................")) {
                "faça algo quando achar Source _ $servidor"
                
                $temp = $Linha.split(" ")
    
                $source=$temp[-1]
                "$servidor $source"
            } 
    
            
        }
    
    }



    Fábio de Paula Junior

    quarta-feira, 21 de agosto de 2013 14:18
    Moderador
  • Vou analisar!

    Obrigado Fábio!



    TulioCass

    quinta-feira, 22 de agosto de 2013 14:49