Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to do the following:

  1. Look for a file in a folder which has been modified in the past 24 hours
  2. Find a specific section of the file
  3. Remove lines from the section matching specific string(s)

Here is the code I have so far:

$SummaryData = gci
| where {([datetime]::now - $_.lastwritetime).TotalHours -lt 24}
| get-content
| select-string 'SUMMARY' -context 0,10
| Where-Object {$_ -notmatch 'Files updated on right side'}

The code above returns no objects. However, if I remove | Where-Object {$_ -notmatch 'Files updated on right side'} it returns the full text:

> SUMMARY
  ------------------------------------------------------------------
  Short Results: 15 copied (203.3GB)
  Operation completed at 22:11:21 on 02/02/2013
  Total duration: 01:01:49

  Copied To Right Side: 15 (203.3GB)
  Files updated on right side : 15

  Transfer amount saved due to partial file updating : 196.2GB
  Remaining actual transfer amount for eligible files: 7.1GB

Can someone tell me where I am going wrong please?

share|improve this question

As usually with PowerShell - what you see is not (exactly) what you get. Data that you try to check with Where-Object is $_.Context.PostContext.

Something like the following worked for me:

$SummaryData = Get-ChildItem | 
    where {(New-TimeSpan -Start $_.LastWriteTime).TotalHours -lt 24} | 
    select-string 'SUMMARY' -context 0,10 | 
    Where-Object {$_.Context.PostContext -notmatch 'Files updated on right side'}

Please, keep in mind that $summaryData will contain objects rather than actual text, so to get text instead you need to modify the command.

EDIT: If you want just text and get rid of the single line you've mentioned:

Get-ChildItem | 
    where {(New-TimeSpan -Start $_.LastWriteTime).TotalHours -lt 24} | 
    select-string 'SUMMARY' -context 0,10 | 
    foreach {@($_.Line) + @($_.Context.PostContext) } | 
    where { $_ -notmatch 'Files updated on right side' }

This builds array of strings from $_.Line (captured line) and $_.Context.PostContext (all that was captured by the -Context parameter), outputs as [String[]], and finally removes the element that you are after.

share|improve this answer
    
Thanks guys for the swift response. BartekB, I couldn't get your specific example to work. However, @Indrek yours did. Both examples prefaced the filename and linenumber to each line for some reason. The only obvious thing which changed was the way the date was calculated. – user195234 Feb 5 '13 at 23:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .