How to make a command write to the file it's reading from?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Just for fun I will try to make a little C program that will store up all it's input from stdin in an array, and when it gets an EOF it will dump everything it stored up and exit.
Just make sure you ave plenty of RAM on the stack ;}
Here it is, but it didn't seem to solve the problem. Here is a little commented command-line session with it:
Code:
$ cat test.txt
buffer is a very silly little program.
$ sed 's:silly:clever:g' test.txt | ./buffer > test.txt
$ cat test.txt
$ # Why didn't it work? Let's test it using a program called "test" that
$ # prints out 5 lines containing the character 'a', over 5 seconds.
$ ./test
a
a
a
a
a
$ # There was a 1 second delay between the appearance of each "a".
$ ./test | ./buffer
a
a
a
a
a
$ # This time it just sat for 5 seconds and then all the "a"'s suddenly
$ # appeared all at once , as expected. I wonder why the "sed" test
$ # didn't work?
buffer basically consists of a loop that gets a character, and if it is not EOF, it appends it to an allocated list. If the list is to small, it is reallocated 50% larger. When the EOF is reached, it prints out the buffer and frees it.
That's just like trying to push a cart while riding it or plugging the wire of an AVR to itself. You'll need another object like a temporary file, a named pipe, an allocated part of the memory, etc. to make that work.
Are you using bash?
bash-3.1$ cat silly
silly
bash-3.1$ echo "$(sed 's/silly/clever/g' < silly)" > silly
bash-3.1$ cat silly
clever
It appears that bash interprets the inner command in $() first, allocate the output as a string then send it to the file before truncating silly. It's a pretty odd or delicate implementation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.