1

I have a log file which has data separated with "|" symbol. Like

"Username|servername|access|password|group"
"Username|servername|access|password|group" 

I need to validate the data. And, If the group column(record) is missing information or empty. I need to write only that row into another file. Please help me. Thanks in Advance.

3 Answers 3

3

If you're just checking for missing data, you can run a quick check using a regex of '(\S+\|){4}\S+'. Use Get-Content with the -ReadCount parameter, and you can work in batches of a few thousand records at a time, minimizing disk i/o and memory usage without going through them one record at a time.

Get-Content $inputfile -ReadCount 2000 |
foreach { 
          $_ -notmatch '(\S+\|){4}\S+' |
          Add-Content $outputfile
         }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the Answer Miolinor, I will try and come back with my results.
2

You could use 'Import-CSV with -Delimiter '|'. If your file doesn't have a header line, you would also need to use -Header to define it. You could then use Where to filter for the empty Group lines and Export-CSV with -Delimiter again to create a new file of just those lines.

For example:

Import-CSV 'YourLog.log' -Delimiter '|' -Header 'Username','Servername','Access','Password','Group' |
    Where {$_.'Group' -eq ''} |
    Export-CSV 'EmptyGroupLines.log' -Delimiter '|'

1 Comment

Thank you for the Answer Mark, I will try and come back with my results.
0

If your group column is always in the same place, which it looks like it is, you could use the split method. You can certainly neaten the code up. I have used the below as an example as to how you could use split.

The foreach statement is to iterate through each line in your file. if (!$($groupstring.Split('|')[4])) checks if it is null.

$groupstring = 'Username|servername|access|password|group'
$groupstring.Split('|')[4]


foreach ($item in $collection)
{
    if (!$($groupstring.Split('|')[4])) 
    {
        Write-Host "variable is null" 
    }
}

Hope this helps. Thanks, Tim.

3 Comments

Thank you for the Answer Tim, I will try and come back with my results.
No worries Subhantech. Looking at my answer, I didn't do an export of the whole line that you asked for. Mark Wragg and @mjoliner look like good answers.
No No Tim, I appreciate your answer. For me there is no sure Answer yet as I am validating all the possibilities. And, I am trying your solution too. Which is good. The first method i did was split method. That is why i didn't check your solutions immediately. I liked Mark's answer as it worked for me. But, there is a long way to go. As my script is a big one and I am learning all the possibilities. The Example i have given in question is just an Abstract of what i want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.