drod
drod

Reputation: 29

Powershell Select-String Remove Carriage Return

I'm trying to select a carriage return in various lines in a report. Eventually, I would like to remove the carriage returns. If I use "`n","`r","\n","\r", I don't get anything for the result. When I access the report in Vi, I can see the carriage return (^L). So I'm not sure what to do at this point but pull my hair out.

ls C:\DOWNLOAD\STANDARD\AUDIT*PRM3711* | %{ gc $_ | Select-String -Pattern '\** WARNING \** -  ' }

Result

** WARNING ** -  SAMPLE REPORT
** WARNING ** -  SAMPLE REPORT
** WARNING ** -  SAMPLE REPORT
** WARNING ** -  SAMPLE REPORT

Upvotes: 2

Views: 16657

Answers (3)

furicle
furicle

Reputation: 1207

Powershell uses

`r`n 

for carriage return line feed

get-content makes an array of strings

So you could use $result = (cat C:\DOWNLOAD\STANDARD\AUDIT*PRM3711* | where { $_ -like '** WARNING ** - ' } ) $result = $result -replace '** WARNING ** - ' , 'rn** WARNING ** - '

Alternatively, I think in PS3 you can make it one long line instead of an array by using get-content -delimiter `0

I don't have a PS3 box in front of me right now....

Upvotes: 0

Emperor XLII
Emperor XLII

Reputation: 13432

gc returns the contents of a file as an array of lines, which will not contain line break characters.

If you need to match across lines, I would suggest the approach from this answer, e.g.:

$content = [IO.File]::ReadAllText( $_.FullName )
if( $content -match "(?s)..." ) {
  # use values in $matches
}

To remove the line break characters from a match, try $matches[0] -replace [Environment]::NewLine or $matches[0] -replace "`r*`n*".

Upvotes: 1

Loïc MICHEL
Loïc MICHEL

Reputation: 26150

in powershell carriage return +line feed shoud be `n

i think you can use this:

[string]::Join("",(gc 'C:\temp\test.txt'))

Upvotes: 0

Related Questions