Reputation: 29
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
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 ** - ' , '
r
n** 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
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
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