tags:

views:

101

answers:

5

Hi,

I am trying to insert some copy-right information into the beginning of some source files. I know that using sed in the following way:

sed "1iSome copyrighted information." sample.txt

would insert the text "Some copyrighted information" to the beginning of sample.txt.
I need to insert text from a file to the beginning of sample.txt. Is there any way in sed that I could use a cat command for the above purpose, say something like the following?:

sed "1i{cat header.txt}" sample.txt

I have googled for the above and have not found exactly what I have been looking for. Any help on this is most welcome.

Thanks.

+3  A: 
cat header.txt sample.txt > temp.txt
mv temp.txt sample.txt
tangens
that will put header underneath sample
ok... but is there a simpler way to do it, maybe with sed?
Sriram
This will append sampte.txt to header.txt. The order is correct. After your comment I tried it again. It works.
tangens
Jason R. Mick
+1  A: 

If you really want to do it using sed:

INFO=$(cat header.txt)  # read the contents of file headers.txt into var INFO
sed -i "1i$INFO" sample.txt # insert $INFO, use -i to change the file inplace

I would however use the cat based method.

codaddict
I think this method wouldn't work if your header contains some strings that sed interprets as a command.
tangens
tangens is right, that does not work. the header.txt contains information in the form of C-style comments (/* and */) which sed interprets as a command.
Sriram
A: 

Why not do this the other way round, e.g. append your source files to your copyright file?

The 'cat' command is actually short for 'concatenate', you can simply say

for f in *.c
do
    cp $f /tmp
    cat copyright.txt /tmp/$f >$f
done
Gaius
You have a major cleanup problem in /tmp - and that assumes you aren't working in /tmp in the first place!
Jonathan Leffler
Not really, that's what /tmp is for. If you're happy you just rm them, if you're not, you've guaranteed that you can undo the change.
Gaius
+1  A: 

Use ed text editor:

echo -e '0r header.txt\nw' | ed sample.txt

or use vi/ex command:

vi - +'0r header.txt|wq' sample.txt

But I don't see any way to run a command in sed.

hluk
+2  A: 

If you have GNU sed:

sed -i -e '2{x;G};1{h;rheader.txt' -e 'd}' sample.txt

sample.txt must be contain at least two lines.

This method works even if the header file contains characters that are special to sed.

Explanation:

  • -i - edit the file in place
  • -e - break the script up into sections - this is necessary in this case to delimit the end of the header filename (it could also be delimited by a newline)
  • 1{h; - on the first line of the file (sample.txt) save it to hold space
  • rheader.txt - read in the header file (header.txt)
  • d} - delete the original first line of the file from pattern space and end processing of line 1 - deleting it here prevents an extra blank line from being inserted at the beginning of the file
  • the header file contents are now output
  • 2{x; - when line 2 of the file (sample.txt) is read, swap it into hold space and swap hold space (containing the original line 1) into pattern space
  • G} - append hold space onto the end of pattern space (which now contains original lines 1 and 2) and complete processing of line 2
  • lines 1 and 2 are now output then processing continues for the rest of the file which consists of simply reading and outputting each line.

Edit: I removed a superfluous command from the version I originally posted.

Dennis Williamson
@Dennis: An explanation of how this works will be great :)
codaddict
@codaddict: You got it.
Dennis Williamson