0

I need to grep particular details from a file and save that to another file.

Here is the file contents :


Name : abc

CollationName : xxxxxxxxx

Edition : System

MaxSizeGB : 5

MaxSizeBytes : 5368709120

ServiceObjectiveName : System

ServiceObjectiveAssignmentStateDescription : Complete

CreationDate : 02-Jul-14 4:14:19 AM

RecoveryPeriodStartDate : 02-Jul-14 4:14:19 AM

Name : def

CollationName : xxxxxxxxxxxxx

Edition : Standard

MaxSizeGB : 250

MaxSizeBytes : 268435456000

ServiceObjectiveName : S2

ServiceObjectiveAssignmentStateDescription : Complete

CreationDate : 25-Jan-15 6:39:41 PM

RecoveryPeriodStartDate : 01-Mar-15 3:00:00 AM

Name : ghi

CollationName : xxxxxxxx

Edition : Standard

MaxSizeGB : 250

MaxSizeBytes : 268435456000

ServiceObjectiveName : S2

ServiceObjectiveAssignmentStateDescription : Complete

CreationDate : 25-Jan-15 3:19:01 PM

RecoveryPeriodStartDate : 01-Mar-15 3:00:00 AM

Name : hjiyt

CollationName : xxxxxxxxxxxxxxxx

Edition : Standard

MaxSizeGB : 250

MaxSizeBytes : 268435456000

ServiceObjectiveName : S2

ServiceObjectiveAssignmentStateDescription : Complete

CreationDate : 11-Mar-15 11:07:30 AM

RecoveryPeriodStartDate : 11-Mar-15 11:37:31 AM


From this file I need to create a file with Name and creation date only. so the output file should be like as follows :

abc 02-Jul-14 4:14:19 AM

def 25-Jan-15 6:39:41 PM

ghi 25-Jan-15 3:19:01 PM

hjiyt 11-Mar-15 11:07:30 AM

It would be great if anyone help me to do this using powershell.

Thanks in advance

1
  • Do you have a script sample that doens't work because of a specific problem? This isn't Amazon for code, we're here to help you with specific problems, not write the code for you. :-) Keywords for solution: Get-Content, about_split, Foreach-Object, regex. Commented Mar 15, 2015 at 11:08

2 Answers 2

1

Assuming you have at least PowerShell 3.0 ( For the -Raw switch of Get-Content. If you don't an easy change can account for that) we can convert that text file into a PowerShell object which would make data manipulation easy.

$fileDataObject = (Get-Content C:\temp\data.log -Raw) -split "\b(?=Name\s+:)" | Select-Object -Skip 1 | ForEach-Object{
    New-Object -TypeName PSObject -Property ($_ -replace "\s:\s","=" | ConvertFrom-StringData)
}

$fileDataObject | ForEach-Object{"{0,-20}{1,-25}" -f $_.Name,$_.CreationDate} | Set-Content "C:\temp\output.txt"

Take the raw data from "C:\temp\data.log" and split the file into chunks where the lines are Name : text. That would return five groups, first one being empty, and we skip the first one.

Those chunks of data are asking to be converted to hash tables using key=value data pairs. We then convert all the colons that are in between white-space into equal signs so satisfy the needs of ConvertFrom-StringData. This transformation is captured into $fileDataObject. You can see its contents below which have been formatted for this answer.

Name  CollationName    Edition  MaxSizeGB MaxSizeBytes ServiceObjectiveName ServiceObjectiveAssignmentStateDescription CreationDate          RecoveryPeriodStartDate
----  -------------    -------  --------- ------------ -------------------- ------------------------------------------ ------------          -----------------------
abc   xxxxxxxxx        System   5         5368709120   System               Complete                                   02-Jul-14 4:14:19 AM  02-Jul-14 4:14:19 AM   
def   xxxxxxxxxxxxx    Standard 250       268435456000 S2                   Complete                                   25-Jan-15 6:39:41 PM  01-Mar-15 3:00:00 AM   
ghi   xxxxxxxx         Standard 250       268435456000 S2                   Complete                                   25-Jan-15 3:19:01 PM  01-Mar-15 3:00:00 AM   
hjiyt xxxxxxxxxxxxxxxx Standard 250       268435456000 S2                   Complete                                   11-Mar-15 11:07:30 AM 11-Mar-15 11:37:31 AM  

Since you just want those 2 columns of data in the output we can arrange that using the format parameter. I saw in the OP that you have 20 character spacing for the Name so that was enforced for the output as well using the -f operator.

Assuming no preexisting data in "C:\temp\output.txt" you should see the output you desire.

Sign up to request clarification or add additional context in comments.

Comments

0
$sourcePath = "C:\test.txt"
$targetPath = "C:\output.txt"

$output = New-Object System.Text.StringBuilder

Get-Content $sourcePath | 
    Where-Object { $_.ToString().StartsWith("Name") -or $_.ToString().StartsWith("CreationDate") } | 
    ForEach-Object { 
        $value = $_.ToString().Split(@(':'), 2, [System.StringSplitOptions]::RemoveEmptyEntries)[1].Trim()
        $temp = New-Object System.DateTime
        $isDateTime = [DateTime]::TryParse($value, [ref]$temp)
        if ($isDateTime) {      
            $output.AppendLine("`t`t`t`t`t$value")
        }
        else {
            $output.Append($value)
        }
    }

Set-Content $targetPath $output.ToString().Trim()

You can improve this where required, but with your sample data shown here it should work.

Comments

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.