Asked  6 Months ago    Answers:  5   Viewed   35 times

What is the simplest way to remove all the carriage returns r from a file in Unix?



I'm going to assume you mean carriage returns (CR, "r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:

$ cat infile

$ cat infile | od -c
0000000   h   e   l   l   o  r  n   g   o   o   d   b   y   e  n

dos2unix is the way to go if it's installed on your system:

$ cat infile | dos2unix -U | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n

If for some reason dos2unix is not available to you, then sed will do it:

$ cat infile | sed 's/r$//' | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n

If for some reason sed is not available to you, then ed will do it, in a complicated way:

$ echo ',s/rn/n/
> w !cat
> Q' | ed infile 2>/dev/null | od -c
0000000   h   e   l   l   o  n   g   o   o   d   b   y   e  n

If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)

Tuesday, June 1, 2021
answered 6 Months ago

I have to conclude that a server setting causes PHP to precede and follow all output with an arbitrary number of carriage returns. Of course, this is an error in the config files of the web host I'm working with. It shouldn't be like this.

Saturday, May 29, 2021
answered 7 Months ago

Its better to use

String lineSep = System.getProperty("line.separator");
Thursday, July 29, 2021
answered 4 Months ago

For reasons unknown, jQuery always converts all newlines in the value of a <textarea> to a single character. That is, if the browser gives it rn for a newline, jQuery makes sure it's just n in the return value of .val(). (Actually the reason probably isn't "unknown"; it's probably to normalize the results across browsers, because IE reports newlines as being 2 characters long.)

Chrome and Firefox both count the length of <textarea> tags the same way for the purposes of "maxlength".

However, the HTTP spec insists that newlines be represented as rn. Thus, jQuery, webkit, and Firefox all get this wrong. When the field is posted, webkit and Firefox correctly add the newlines!

The upshot is that "maxlength" on <textarea> tags is pretty much useless if your server-side code really has a fixed maximum size for a field value.

Edit This is still an issue in 2015 - at least on Chrome 45.0.2454 and IE 11.0.9600.

Monday, August 9, 2021
answered 4 Months ago

You're reading the file line by line, so the line returned will never contain r, n or rn - as those are line separators.

The simplest approach is just to load the whole lot into memory, remove all the line breaks, then write it out:

string text = File.ReadAllText(sourceFileName);
text = text.Replace("r", "").Replace("n", "");
File.WriteAllText(sourceFileName, text);

Of course if you only want to remove carriage returns, just remove the .Replace("n", "") in the above code.

EDIT: Now that we can see your file, there is no XML version 10, which suggests that the document is corrupt before you even get it. You should stop at this point and work backwards towards the source until you can get a valid XML file. There's no point in continuing with what you've currently got.

Thursday, November 18, 2021
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :