none
Replace function and wildcards

    Question

  • Hi all,

    i'm tryng to extract from a log file only the lines with specific text.
    I'm look for each lines in the log file where i find "@todomain.it" string in a specified position,  I explain:
    the log file's lines structure is composed to much information and appear like this:
    date-time-serverName-senderAddress-recipientAddress-relayHost
    so i need to extract only the lines where the recipientAddress contains the domain "@todomain.it"

    To do this I specify the domain to find in variable $DominioDaCercare using wildcards to specify it "*@todomain.it*" because without wildcards the first filtering of the log file do not return nothing.
    After the first filtering i want to extract only the raws where recipientAddress field is "*@todomain.it*" but using wildcards the script search for string "todomain.it*" and trying to remove "*" using replace function it doesn't works...
    what can i do to solve this ?

    Here the script:

     

    $a = [char]9
    <# la funzione char restituisce il carattere corrispondente al numero presente dopo la funzione #>
    <# [char]9 restituisce il carattere corrispondente al codice ASCII 9 che è la tabulazione #>

    $DominioDaCercare = "*@todomain.it*"

    $intestazione = (Get-Content -Path C:\LogRelay\20111220.log -TotalCount 3)[-1]
    <# -TotalCount legge il numero di righe specificato di seguito mentre l'opzione [-1] restituisce l'ultima riga letta #>
    <# in questo modo la variabile $intestazione contiene la riga con le intestazioni di colonna del file di log #>

    <# tolgo dall'intestazione i primi 2 caratteri che sono # ed uno spazio #>
    $intestazione = $intestazione -replace("#","")
    $intestazione = $intestazione -replace(" ","")
    $intestazione = $intestazione -replace($a,";")

    $intestazione | Out-File C:\LogRelay\Export.csv

    $testo = Get-Content -Path "C:\LogRelay\20111220.log" | where { $_ -like $DominioDaCercare}

    $testo = ForEach ($line in $testo){$line -replace(";",",")}

    $testo = ForEach ($line in $testo){$line -replace($a,";")}

    $testo | Out-File C:\LogRelay\testo.txt

    <# to here all works correctly #>

        ForEach ($line in $testo)
            {
                $arrLine = $line.split(";")           
                $DestinatarioNelLog = $arrLine[7]  <# this is the field where in tthe log is reported the recipent address #>
                $arrDominioNelLog = $DestinatarioNelLog.Split("@") <# i split the recipient address to obtain only the domain #>
                $DominioNelLog = $arrDominioNelLog[1]
                $arrDominioDaCercare = $DominioDaCercare.Split("@") <# i split also the domain to find to check only the part after @ #>
                Out-Host -InputObject $arrDominioDaCercare[1]

                <# HERE IS THE PROBLEM #>
                <#  I'M TRYING TO REMOVE "*" FROM THE STRING TO FIND BECAUSE WITHOUT REMOVING IT IN THE NEXT HOPE WHERE I CHECK IF THE TWO STRINGS ARE EQUAL THE TEST FAIL FOR THE "*" PRESENT #>
                $arrDominioDaCercare[1] = $arrDominioDaCercare[1] -replace("*","")
                $DominioDaCercare = $arrDominioDaCercare[1]
                $arrDominioDaCercare[1] | Out-File C:\LogRelay\dominiodacercare.txt
               
                Read-Host

    <# TEST DOESN'T WORKS BECAUSE THE FIRST STRING IS "DOMAINTOFIND.IT" AND THE OTHER STRING IS "DOMAINTOFIND.IT*"
                if ($DominioNelLog -eq $DominioDaCercare)
                    {
                        $line | Out-File -Append C:\LogRelay\Exporteq.csv
                        $line | Out-File -Append C:\LogRelay\lineeq.txt
                    }           
            }

     

     

    Monday, January 16, 2012 3:39 PM

Answers

All replies