Hi all: If I do: $echo 'lalal' >> /tmp/some_file bash appends lalal to the END of the file. How can I make it so bash adds lalal to the BEGINING of the file? Thank you? PS: I've done it creating a temp file and using >> again, but I want to do it with just one command.
On Fri, 2 May 2003 14:51:30 -0300
"Martin Guillen"
If I do: $echo 'lalal' >> /tmp/some_file bash appends lalal to the END of the file.
How can I make it so bash adds lalal to the BEGINING of the file?
Here is a couple of ideas for you. If the text is always the same: ####################################### #!/usr/bin/perl -pi.orig #usage: head-prepend filename.txt # will prepend "Hello Dolly" to filename.txt #will not work on zero length file $_ = "lalal\n" . $_ unless $done++; ####################################### For varying text: ####################################### #!/usr/bin/perl #useage: header file 'text message' # text should be quoted if it has spaces $file= $ARGV[0]; $text = $ARGV[1]; open (FH,"+< $file") or warn $!; @file =(<FH>); close FH; unshift @file, "$text\n"; open (FH,">$file")or die "Cant open: $!"; print FH @file; close FH; ########################################## -- use Perl; #powerful programmable prestidigitation
On Friday 02 May 2003 6:51 pm, Martin Guillen wrote:
Hi all:
If I do: $echo 'lalal' >> /tmp/some_file bash appends lalal to the END of the file.
How can I make it so bash adds lalal to the BEGINING of the file?
Thank you?
PS: I've done it creating a temp file and using >> again, but I want to do it with just one command.
Short answer, you probably can't. Without being any kind of shell expert, let me try and explain what you are trying to do, so that you understand the problem. The explanation is a bit simplified, so I fully expect that someone will say I've got some details wrong, but never mind, the principles stand for dealing with your Q. A file exists in a directory and is a datastructure which contains 2 kinds of data. 1] the payload data, which is what you would normally consider to be the file, and which is transferred when the file is copied or sent somewhere. 2] location data, which says where the data is located on the storage medium, which is never transmitted, as it is only relevant to the location where the file is stored. The location data would be arranged in the form of a linked list [of which there are many variants], which will consist of 1 or more elements. Each element consists of 2 pointers [address structures], the first pointing to a block of payload data, and the second pointing to the next linked list element. The first element is effectively the file entry in the directory. The last element points to the data at the end of the file and to NULL as the next element [ie there is no next element]. When you do: $echo 'lalal' >> /tmp/some_file, this last element is found, and your text appended to the block if there is space, or a new link list element added and your text written to the new block. The linked list scheme allows files to be appended to at the end very easily, without disturbing existing content. For what you are proposing [adding data at the start of the file], you would have to shuffle all the data by 5 bytes [plus delimiter characters] in the first block [plus delimiter characters], and transfer those 5 bytes to the next block and so on to the end of the file. As you have discovered, you need a temp file to do this. HTH Vince Littler
participants (3)
-
Martin Guillen
-
Vince Littler
-
zentara