Comment on Why does not a CR (Carriage Return) automatically start a new line on some online text editors?
historicaldocuments@lemmy.world 1 week agoThere is Carriage Return (CR), and also Line Feed (LF, often called New Line). If you think about old mechanical printers with the metal arm sticking out, a CR operation would move the type head to the far left column, and a LF operation would advance the paper by one line. Variously through the years depending on hardware (typewriter, teletype, those early CRTs that you had to refresh the screen, or modern computers) you would get one or both of those if you pressed Return/Enter, and it’s configurable in software, depending on the software. I don’t know what windows does these days with notepad, but at one time the Enter key sent both (CRLF). UNIX style systems tended to use LF, and older Macs as someone else referenced used CR. If you wrote a generic program to handle anything you had to account for all of them. Mostly these days it gets abstracted away which generally works well enough unless a team of people used a random collection of software to edit a text file.
printf "\r\nHexadecimal, like that scene from The Martian.\n" | hexdump -C 00000000 0d 0a 48 65 78 61 64 65 63 69 6d 61 6c 2c 20 6c |..Hexadecimal, l| 00000010 69 6b 65 20 74 68 61 74 20 73 63 65 6e 65 20 66 |ike that scene f| 00000020 72 6f 6d 20 54 68 65 20 4d 61 72 74 69 61 6e 2e |rom The Martian.| 00000030 0a |.| 00000031
The 0a is a Line Feed character, and the 0d is a Carriage Return character. In my terminal without piping it through hexdump you get:
printf "\r\nHexadecimal, like that scene from The Martian.\n" Hexadecimal, like that scene from The Martian.
The LF at the end of the string makes it so that the prompt at the terminal doesn’t appear on the same line as the output, and the blank line before the text is caused by the LF at the beginning. I don’t know/care/have to worry about what eats the CR.
schipelblorp@sh.itjust.works 1 week ago
Ah! Thank you for explaining it in terms of typewriters, now it’s much clearer in my mind!
The confusion still lingers. I used to cut and paste websites to print. One part of the condensation process, in addition to removing ads, was removing all the two-space line breaks of CRCR or LFLF; I still have to search for both.
historicaldocuments@lemmy.world 1 week ago
You could probably get 80% of that process done by learning some python. If you have a string “s”, then replacing double newlines with a single newline is as easy as
re.sub(“\n\n”, “\n”, s)where “\n” is an LF in many programming languages. A CR is often “\r” in the same vein. Just be aware that regular expressions can be very, very frustrating; and every webpage is going to be a new adventure in how it got formatted. If you use something like spyder it’ll allow you to see what the data looks like inside the python process so you get a chance to iterate.