0

I have a simple text file that looks like this...

A,400000051115,null,null,null,null,null,null,null,20190312,090300,Answer Machine,2019,3,14,10,0
A,400000051117,null,null,null,null,null,null,null,20190312,090300,Confirmed,2019,3,14,10,30
A,400000051116,null,null,null,null,null,null,null,20190312,090300,Answer Machine,2019,3,14,11,0
A,400000051114,null,null,null,null,null,null,null,20190312,090300,Wants to Cancel,2019,3,14,9,0
A,400000051117,null,null,null,null,null,null,null,20190312,091800,SMS Sent,2019,3,14,10,30
A,400000051116,null,null,null,null,null,null,null,20190312,091800,SMS Sent,2019,3,14,11,0
A,400000051115,null,null,null,null,null,null,null,20190312,091800,SMS Sent,2019,3,14,10,0
A,400000051116,null,null,null,null,null,null,null,20190312,093000,Appointment Cancelled/Rescheduled Via SMS,2019,3,14,11,0

I need to save all the lines except those that have "SMS Sent" in them to a new file. I am using the following...

get-content $SourceFile.FullName | select-string -pattern 'SMS Sent' -notmatch | Out-File $targetFile

Why in the resulting file do I get a blank first line?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • Did my answer [solve your problem or get you on the right track](https://stackoverflow.com/help/someone-answers), or was something further needed? – Lance U. Matthews Apr 05 '19 at 20:00

1 Answers1

0

If you change Out-File $targetFile to Out-Host or even just omit that last segment in the pipeline, you will see a blank line in the console output, too.

The output analog of Get-Content is Set-Content, so if you change Out-File $targetFile to Set-Content $targetFile the first line is no longer blank.

Also, since you're working with a CSV file you could use Import-CSV to read the data and Where-Object to filter on that specific column, although a little extra work is required to specify the headers and omit them from the output file...

$csvHeaders = 1..17 | ForEach-Object -Process { "Column $_" }
$csvHeaders[11] = 'Status'

Import-Csv -Path $SourceFile.FullName -Header $csvHeaders `
    | Where-Object -Property 'Status' -NE -Value 'SMS Sent' `
    | ConvertTo-Csv -NoTypeInformation `
    | Select-Object -Skip 1 `
    | Set-Content $targetFile

...which writes...

"A","400000051115","null","null","null","null","null","null","null","20190312","090300","Answer Machine","2019","3","14","10","0"
"A","400000051117","null","null","null","null","null","null","null","20190312","090300","Confirmed","2019","3","14","10","30"
"A","400000051116","null","null","null","null","null","null","null","20190312","090300","Answer Machine","2019","3","14","11","0"
"A","400000051114","null","null","null","null","null","null","null","20190312","090300","Wants to Cancel","2019","3","14","9","0"
"A","400000051116","null","null","null","null","null","null","null","20190312","093000","Appointment Cancelled/Rescheduled Via SMS","2019","3","14","11","0"

...to $targetFile. Note that all of the values are quoted now. If your input file does have headers then you could use simply...

Import-Csv -Path $SourceFile.FullName `
    | Where-Object -Property 'Status' -NE -Value 'SMS Sent' `
    | Export-Csv -NoTypeInformation -LiteralPath $targetFile

In either case the output file will not contain a leading blank line.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • I have a deja vue ;-) If you leave the pipe symbol as last char of a line, the backticks aren't necessary. –  Mar 21 '19 at 22:15
  • I prefer the pipe symbol at the beginning of the line so it's obvious it's a continuation and doesn't get overlooked on long lines, which is why it's written with backticks. – Lance U. Matthews Mar 21 '19 at 22:30