I'm having trouble redirecting output to a file. Here's my example: I want to read in an existing file, replace all single newlines with two newlines, and save it. Here's what I've got:
Code:
cat -E documents/file.txt | sed 's/\$/\n/g' > documents/file.txt
However, when I open up file.txt, it's empty. What gives? Do I have to 'close' it, or something like that?
(This post was last modified: 18/01/2013 06:12 AM by whjms.)
(18/01/2013 08:13 AM)Joom Wrote: You don't have to open the file with cat.
I tried using the '<' symbol (not sure what it's called), but I need a way to read every newline. The -E option displays a '$' before every newline. Is there a more efficient way to show non-printable characters?
(18/01/2013 09:54 AM)trademark91 Wrote: Save it to file2.txt you could be overwriting it each time. You should never overwrite a file with itself.
So is it just an issue with output redirection? If so, I guess I could make the new file, delete the old, and rename the new one.
18/01/2013 12:28 PM
ZiNgA BuRgA
Smart Alternative
Posts: 17,022.2988 Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391 E-Pigs: 446.1274
I think the > operator immediately truncates the file before cat can even open the file for reading.
I'd imagine that if cat would open the file for reading first, the line would actually work, due to how Linux handles reads and writes to a file at the same time.
Using the >> operator instead does work as expected, though this isn't what you want.
This explains most of the stuff, but is still pretty poor documentation (even though the author says ITS THE BEST EVRRRR) http://www.grymoire.com/Unix/Said.html
MY SIG IS FUCKING DEAD
(This post was last modified: 18/01/2013 09:56 PM by Tetris999.)