How to append text at the beginnig of a file?
Dear all, how can I append a text header to a plain-text file (source code) without deleting what's been already there? I guess, the clue resides on 'man sed' but some examples will be much appreciated. Thanks, Martin
cat header_file >temp_file cat plain_text >>temp_file mv temp_file plain_text Noting fancy but it should work. Regards, Terry Martin Mielke wrote:
Dear all,
how can I append a text header to a plain-text file (source code) without deleting what's been already there?
cat header body > tempfile; mv tempfile body Quoting Martin Mielke <martinm@people-com.com>:
Dear all,
how can I append a text header to a plain-text file (source code) without deleting what's been already there?
I guess, the clue resides on 'man sed' but some examples will be much appreciated.
Thanks,
Martin
-- I don't do Windows and I don't come to work before nine. -- Johnny Paycheck
Martin Mielke wrote:
Dear all,
how can I append a text header to a plain-text file (source code) without deleting what's been already there?
I guess, the clue resides on 'man sed' but some examples will be much appreciated.
You can do it with the sed insert command, but a temporary file cannot be avoided. Create a script (called for example `insert_header') with the following contents: #!/usr/bin/sed -f 1i\ First line\ Second line The so called "hash-bang" (#!) invokes sed with the remainder of the script as commands for sed. Make it executable: chmod u+x insert_header Invoke it with the file to read as the argument: insert_header my_file Note that the output goes to stdout. To make it complete, do something like: insert_header my_file > tmp_file && mv tmp_file my_file The `&&' makes sure that the second command (`mv') only takes place if the first command is succesful. It pays off to always take such a precation. After all, it is hardly any trouble, but it could safe you a lot of grief. Good luck. Paul.
On Thu, Jan 25, 2001 at 09:44:17PM +0100, P.C. Uiterlinden wrote:
Martin Mielke wrote:
Dear all,
how can I append a text header to a plain-text file (source code) without deleting what's been already there?
I guess, the clue resides on 'man sed' but some examples will be much appreciated.
Here is a novel solution for you. Suppose you want to prepend (I dont think you can append to the beginning of something) Prepend contents of file1 before contents of file2 CF2=`cat file2`;cat file1 >file2;echo $CF2>>file2 Might be a limit to how much data a shell var will hold :) Or more elegantly, suppose you have a bunch of 'c' files and you want to prepend a file called header. # prepend header to 'c' files for FILE in *.c; do ed -s $FILE <<EOF 0r header w q EOF done Cliff
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 or simply: tosi@dustpuppy:~ > echo "1 2 3" >a tosi@dustpuppy:~ > echo "4 5 6" >b tosi@dustpuppy:~ > ( cat b a ) | cat > b tosi@dustpuppy:~ > cat b 4 5 6 1 2 3 tosi@dustpuppy:~ > :-)
On Thu, Jan 25, 2001 at 09:44:17PM +0100, P.C. Uiterlinden wrote:
Martin Mielke wrote:
Dear all,
how can I append a text header to a plain-text file (source code) without deleting what's been already there?
I guess, the clue resides on 'man sed' but some examples will be much appreciated.
Here is a novel solution for you. Suppose you want to prepend (I dont think you can append to the beginning of something) Prepend contents of file1 before contents of file2
CF2=`cat file2`;cat file1 >file2;echo $CF2>>file2
Might be a limit to how much data a shell var will hold :)
Or more elegantly, suppose you have a bunch of 'c' files and you want to prepend a file called header.
# prepend header to 'c' files for FILE in *.c; do ed -s $FILE <<EOF 0r header w q EOF done
Cliff
- -- ______ /---------------------------------------\ \ | Þór Sigurðsson | Tor Sigurdsson | t | | Netmaður | Network Specialist | o | |-----------------------------------------| s | | tosi@rhi.hi.is | i | \---------------------------------------/_____/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.1e-SuSE (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE6cMGd6mRH+PEpr2YRAiJgAJ9/fO1HNHp/817+VHc2X1Zrk7E47gCeNQ/8 w1Hc+/WNiw3UatomsBj0F8s= =phJs -----END PGP SIGNATURE-----
participants (6)
-
Cliff Sarginson
-
Jeffrey Taylor
-
Martin Mielke
-
P.C. Uiterlinden
-
Terry Eck
-
Tor Sigurdsson