[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6199
  • Last Modified:

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.
1
evetsleep
Asked:
evetsleep
  • 4
  • 3
  • 2
1 Solution
 
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
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
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
 
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

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now