6 ms·
The newline cat mystery
- gnfargbl 3y agoxxd
- skrebbel 3y agoI’m impressed that there are people who mix what I think is Visual Basic (right?) with UNIX command line tools. Hadn’t seen that one coming!
- DonHopkins 3y agoI used to leave a file called README on my public ftp directory, which contained only: cat: README: No such file or directory I'd occasionally get email from frustrated people who had trouble trying to read the README file, so I'd tell them to simply run "emacs README", and emacs would solve all of their problems. I don't know if my passive aggressive emacs evangelism ever worked, because I never heard back from them.
- h2odragon 3y ago"cat wasn't displaying anything" ? perhaps: "the terminal correctly displayed the file, overwriting the printed line at each CR, with the last line blank" https://unix.stackexchange.com/questions/355559/bash-and-carriage-return-behavior https://unix.stackexchange.com/questions/355559/bash-and-car...
- benj111 3y agoThat over writes though. It doesn't delete. Foofoo\rbar\r Should show as barfoo With the cursor over the start of that line.
- dale_glass 3y agoIf there's no newline, cat won't output one, and bash will put the prompt right after: [dale@host ~]$ echo -n "hi" > test [dale@host ~]$ cat test hi[dale@host ~]$ If you add a \r, then the cursor goes back to the start of the line, and the prompt overwrites the "hi". [dale@host ~]$ echo -en "hi\r" > test [dale@host ~]$ cat test [dale@host ~]$
- h2odragon 3y agodepends on the terminal
- jstanley 3y agoWhat terminal interprets "\r" as "clear the line"? I would have thought that is considered a bug.
- fragmede 3y agomost of them do. it's what a /r means. it's how cli programs can easily output a spinning wheel/similar for busy status.
- jstanley 3y agoNo, "\r" means return the cursor to the start of the line without clearing the line. Try this: $ echo -ne "ooo\rf\n" foo If a "\r" cleared the line, you would just see a "f", not a "foo".
- fragmede 3y agoisn't that what I said?
- t-3 3y agoYou want to know how I know you never wrote your own text editor? Carriage return is a carriage return, not a delete or backspace or clear line.
- dan-robertson 3y agoYeah except the file ends in \r so the OP may have had their prompt longer than any of the lines (e.g. [user@hostname cwd] $) such that they couldn’t tell.
- deleted 3y ago[deleted]
- thequux 3y agoI'm a big fan of `sed -nel` for an unambiguous listing of the contents of a file. The only case that sed doesn't show is a missing trailing linefeed.
- eichin 3y agoI use "cat --show-all" at least partly because it's more teachable (since when I run into this I'm usually helping untangle someone who hasn't run into the problem before, and it's more explicitly about the problem instead of being a related thing that happens to expose it (like this, or hexdump)
- ramses0 3y agohexdump -C ...this is the only way.
- fsckboy 3y agowhy would you show a missing trailing linefeed? it's missing... do you mean it will do a single CR-LF whether there was a NL there or not?
- okanat 3y agoThis is one of the places that "do one thing" principle breaks down. Or a bad habit became a bad definition for the use case of a command and it stuck for newbies. `cat` usually does one thing: concatenate the contents of the inputs you give in the order you give them. Of course, if the user gives only one input, it will only print that. It is not a text viewer even though many *nix users use it that way because they learned it that way, because a series of tutorials taught the wrong thing to generations. The very first one probably came from a very limited and very bad Unix environment like a server that is hostile to active development. So the tutorial used whatever core utility that's available: cat, beginning the multi-generational bad habit.
- probably_wrong 3y agoI would like to counter-argue that it's the author's fault for doing things the wrong way. While I'm sure the actual code looked different, the "spirit" of the code builds a CSV file by concatenating strings and specifying raw ASCII codes. Building a CSV file like this on 2023 is guaranteed to cause bugs down the line. Had the code used the Python CSV primitives, or at least had the author let Python convert the "\n" character to the OS-dependent representation, the bug would have been avoided entirely. The author just happened to run into it with `cat` instead of, say, a CSV parser confused about the unusual choice.
- kevin_thibedeau 3y agoIt's ColdFusion, not Python. A language that forces you to program like it's the 60s because character escapes are too advanced a concept to implement.
- kevincox 3y agoThe author says it was a simplified example. They may will have implement a fully features CSV emitter but got the liver separator wrong. Either way whether or not their CSV was valid is irrelevant to the story. The point is that some characters in a file may obscure the contents of that file in surprising ways.
- Qwertious 3y ago
- h2odragon 3y agoThis awakened dim memories of trying to duplicate curses functionality and reading sources for that and termcap. share and enjoy https://www.gnu.org/software/termutils/manual/termcap-1.3/html_chapter/termcap_2.html https://www.gnu.org/software/termutils/manual/termcap-1.3/ht...
- gumby 3y agoI don’t understand this part: > Now one thing that I should have noticed was that the file size was 9 bytes The file contains more than 9 characters. What was the author trying to say?
- xelxebar 3y agoOutput that doesn't end in LF is pretty annoying in general, since the shell prompt gets visually tacked on to the end. I have this as part of my PROMPT_COMMAND: printf "%%%$((COLUMNS - 1))s\\r%1s\\r" Effectively, it adds the missing newline in those cases and puts a '%' at the end of the output to let you know. The implementation is tad bit of printf trickery that leverages your bash's line wrapping.
- masklinn 3y agoIIRC ZSH auto-fixes partial lines, and can display a marker if you enable PROMPT_SP (I don't think it's enabled by default): > Attempt to preserve a partial line (i.e. a line that did not end with a newline) that would otherwise be covered up by the command prompt due to the PROMPT_CR option. This works by outputting some cursor-control characters, including a series of spaces, that should make the terminal wrap to the next line when a partial line is present (note that this is only successful if your terminal has automatic margins, which is typical). > When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a ‘%’ for a normal user or a ‘#’ for root. If set, the shell parameter PROMPT_EOL_MARK can be used to customize how the end of partial lines are shown.
- deleted 3y ago[deleted]
- dmarinus 3y agocat does have parameters to show nonprinting characters.
- pronoiac 3y agocat -v For a very short file, I might reach for hexdump -C
- jolmg 3y ago> Still I find it very odd that cat didn't show anything, perhaps it is because the carriage return, brings the cursor back to the beginning of the line, effectively erasing the line that was just output. Bringing the cursor back doesn't delete what was output. It's writing over it with the second line and the shell prompt that deletes it. If you set `PS1='$ '`, one should be able to see part of both lines: $ printf '"order_id","date"\r"1","2023-01-01"\r' $ ","2023-01-01""
- andrelaszlo 3y agoI usually pipe to od (od -c) whenever I'm not sure about stuff like that.
- fragmede 3y agoor hd (hexdump -C)