Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Adding text to the beginning of a file



On 16/03/07, Dave M G <martin@example.com> wrote:
TLUG,

I'm writing a bash shell which will write today's date and some other
info at the top of a bunch of PHP files.

I believe that ordinarily, the command "cat" is used for this. However,
unless I'm mistaken, "cat" will always add text to the beginning of the
file.

What I'll need to do is to over write some text up to a particular point.

So, if at the top of my PHP file it says:

# File modified 2007 03 16
# by Dave M G
# --

Can I wipe out everything up to and including the "--" and then past in
new text?


Sed is your friend:

sed '1,/^#--/ d' < file_with_data_to_replace > tmp_file
cat file_with_new_data tmp_file > end_result_file

The 1st line deletes everything from line 1, up until it finds a '#--'
at the start of the line.
The 2nd line just pastes the new data together with the old.

A nice tutorial on sed can be found here:
http://www.grymoire.com/Unix/Sed.html

Arwyn

PS Hello everyone! This is my first post to the list, yoroshiku.


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links