Powershell Select-String Remove Carriage Return

14,104

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*".

Share:
14,104
drod
Author by

drod

Updated on June 04, 2022

Comments

  • drod
    drod almost 2 years

    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