Post Reply 
Linux/Bash help - redirection
Author Message
whjms
EpiCanadian

Posts: 435.1825
Threads: 126
Joined: 23rd Dec 2008
Reputation: 3.37413
E-Pigs: 24.8045
Offline
Post: #1
Linux/Bash help - redirection
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 06:10 AM
Find all posts by this user Quote this message in a reply
Joom
WOOP
Worlds End

Posts: 4,206.7320
Threads: 417
Joined: 20th Mar 2009
Reputation: 5.41709
E-Pigs: 134.1772
Offline
Post: #2
RE: Linux/Bash help - redirection
You don't have to open the file with cat.

[Image: ROVBdMh.png]
3DS Friend Code: 5000-6045-4964
18/01/2013 08:13 AM
Find all posts by this user Quote this message in a reply
trademark91
Unique?
Fractal Insanity

Posts: 4,719.9300
Threads: 269
Joined: 4th Jan 2008
Reputation: -6.15982
E-Pigs: 105.8691
Offline
Post: #3
RE: Linux/Bash help - redirection
Save it to file2.txt you could be overwriting it each time. You should never overwrite a file with itself.

[Image: 531115][Image: 76561198014212040.png]
windows Proud
18/01/2013 09:54 AM
Find all posts by this user Quote this message in a reply
whjms
EpiCanadian

Posts: 435.1825
Threads: 126
Joined: 23rd Dec 2008
Reputation: 3.37413
E-Pigs: 24.8045
Offline
Post: #4
RE: Linux/Bash help - redirection
(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
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,022.2988
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.1274
Offline
Post: #5
RE: Linux/Bash help - redirection
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.
18/01/2013 08:26 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Tetris999
..............................

Posts: 2,390.4622
Threads: 298
Joined: 15th Apr 2007
Reputation: -6.7936
E-Pigs: 82.5657
Offline
Post: #6
RE: Linux/Bash help - redirection
Here's something that can perform what you want:

Code:
sed -i ':a;N;$!ba;s/\n/\n\n/g' test.txt

copied from: http://stackoverflow.com/questions/12519...-newline-n

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.)
18/01/2013 09:00 PM
Find all posts by this user Quote this message in a reply
lembas
yuri princess

Posts: 4,365.2931
Threads: 184
Joined: 30th Apr 2008
Reputation: -2.56923
E-Pigs: 154.3525
Offline
Post: #7
RE: Linux/Bash help - redirection
You're just doing a lot of erroneous spoon here.

There's no need for cat, and no need to output to the file.

said 'blah' file will do it just fine.

I'm gay.
Spoiler for online game stuff:
[Image: 76561198003967361.png]
[Image: thelemmingbas.png]
3DS FC: 4184-1632-9353
Spoiler:
[Image: wQZsI.jpg]
Spoiler:
In the words of the wise trademark91:
windows Proud
18/01/2013 09:49 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: