Remove carriage returns from text files

I need to produce text files that will be used by UNIX systems.  These systems don't understand CRLF at the end of each line, such as how Windows likes to end each line.  I'm looking for a way to remove the carriage returns so that each line ends with a line feed (LF) only.
LVL 1
evetsleepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chops_ukCommented:
You could find a hex editor (http://en.wikipedia.org/wiki/Comparison_of_hex_editors) and manually remove the 0D0An (CRLF characters) manually.
0
evetsleepAuthor Commented:
I was hoping to do it natively via PowerShell, suppose I should have mentioned that.  Currently I'm using ConvertTo-UnixLineEnding cmdlet from  the PowerShell Community Extensions, but I'd like to do it without having to install\load PSCX.
0
chops_ukCommented:
how about something using
[System.Text.RegularExpressions.Regex]::Replace($Text,"0D0A"," ");
or
Get-Content yourfile.csv |% {$_-replace "t`n", "ting`na "}
0
INTRODUCING: WatchGuard's New MFA Solution

WatchGuard is proud to announce the launch of AuthPoint, a powerful, yet simple, Cloud-based MFA service designed to eliminate the vulnerabilities that put your data, systems, and users at risk.

evetsleepAuthor Commented:
So if the file is:

This is line 1
This is line 2
This is line 3

With each one ending in CRLF (`r`n), I tried:

get-content test.txt | foreach { [System.Text.RegularExpressions.Regex]::Replace($_,"0D0A"," ") } | out-file test.txt

In your next suggestion you're just doing string replacement and not pulling `r out of `r`n at the end of each line..not sure where replacing t with ing came in..  I did, however try:

Get-Content test.txt | foreach {$_ -replace "`r`n',"`n"} | out-file result.txt

But that didn't work either.
0
Chris DentPowerShell DeveloperCommented:

Get-Content reads a line at a time, Out-file writes a line at a time. You'll need to be a bit deeper than those to do this.

I'll prod things around and see if anything turns up.

Chris
0
Chris DentPowerShell DeveloperCommented:

Well there are potentially a few different ways to approach this. This one has the advantage of being very short, but it may not scale well if you need to do this for extremely large files.

If size is an issue we can gain a lot by switching Get-Content to System.IO.StreamReader and writing lines to the other file immediately rather than loading the source file into memory first.

Of course, that assumes this bit works for you in the first place :)

Chris
$InputFile = "test.txt"
$OutputFile = "out.txt"

$Writer = New-Object IO.StreamWriter "$($PWD.Path)\$OutputFile"
$Writer.Write( [String]::Join("`n", $(Get-Content $InputFile)) )
$Writer.Close()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evetsleepAuthor Commented:
These files are all relatively small (less then 10kb each), so this would work perfectly.  If I currently have the data I want in an array could I do this:


#$data <--contains an array of strings
$OutputFile = "out.txt"

$Writer = New-Object IO.StreamWriter "$($PWD.Path)\$OutputFile"
$Writer.Write( [String]::Join("`n", $data) )
$Writer.Close()

Open in new window

0
Chris DentPowerShell DeveloperCommented:

Yes, exactly :)

Chris
0
evetsleepAuthor Commented:
Just tested and looks like the answer is a YES....thanks a TON for the help..this has helped me a lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.