LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-07-2010, 03:07 PM   #16
kofucii
Member
 
Registered: May 2007
Location: Bulgaria
Distribution: Slackware, SCO Unix
Posts: 62

Rep: Reputation: 20

Also it can be done with "perl", if you insist on one command solution:

Code:
perl -pi -e "s/foo/bar/g" myfile
 
Old 01-07-2010, 03:08 PM   #17
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
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 ;}

Last edited by Tinkster; 01-07-2010 at 03:09 PM.
 
Old 01-07-2010, 03:36 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.

Here is the C source code for buffer:

Last edited by MTK358; 03-26-2010 at 04:18 PM.
 
Old 01-07-2010, 04:57 PM   #19
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
echo "$(sed 's/silly/clever/g' < silly)" > silly

This seems to work file is closed before opening for writing i think.
 
Old 01-07-2010, 05:07 PM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by whizje View Post
echo "$(sed 's/silly/clever/g' < silly)" > silly

This seems to work file is closed before opening for writing i think.
That not only clears the file but it also complains about it not existing.
 
Old 01-07-2010, 05:24 PM   #21
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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
 
Old 01-07-2010, 07:27 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Are you using bash?
Yes, sure.

EDIT: I tried your example and it worked this time.

Last edited by MTK358; 01-07-2010 at 07:30 PM.
 
Old 01-07-2010, 08:28 PM   #23
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 01-07-2010, 08:36 PM   #24
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by whizje View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script passwd command by reading passwords from a file redleg7 Linux - General 1 01-12-2009 09:57 PM
reading a Windows Write file newbiesforever Linux - Software 15 02-12-2008 03:59 PM
Reading a file and running a command for each line. mijohnst Linux - General 11 08-22-2005 06:18 PM
Pop3 Command stream end of file while reading line sesimonsen Linux - Networking 0 05-13-2005 12:29 PM
I accidentally deleted make file in /usr/local/bin, now cannot use make command.... Niceman2005 Linux - Software 2 11-17-2004 07:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration