Inserting text from a file at the beginning of another file - bash

I am trying to insert some copy information at the beginning of some source files. I know using sed as follows:

sed "1iSome copyrighted information." sample.txt 

will insert the text “Some copyrighted information” at the beginning of sample.txt.
I need to insert text from a file at the beginning of sample.txt. Is there any way in sed that I could use the cat for the specified purpose, say something like this:

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

I have googled for the above and did not find exactly what I was looking for. Any help on this is welcome.

Thanks.

+7
bash insert sed
source share
6 answers

Use the ed text editor:

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

or use the vi / ex command:

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

But I do not see the ability to run the command in sed.

+8
source share

If you have GNU sed :

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

sample.txt must contain at least two lines.

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

Explanation:

  • -i - edit the file in place
  • -e - partition the script - this is necessary in this case to delimit the end of the header file name (it can also be separated by a newline)
  • 1{h; - in the first line of the file (sample.txt) save it to store a space
  • rheader.txt - read in the header file (header.txt)
  • d} - delete the initial first line of the file from the template space and finish processing line 1 - deleting it here prevents the insertion of an additional empty line at the beginning of the file
  • header file content is now displayed
  • 2{x; - when reading line 2 of the file (sample.txt), change it to hold and replace the free space (containing the original line 1) with the drawing space
  • G} - add a hold space to the end of the pattern space (which now contains the source lines 1 and 2) and finish processing line 2
  • lines 1 and 2 are now output, then processing continues for the rest of the file, which consists in simple reading and output of each line.

Edit: I removed the extra command from the originally published version.

+8
source share

Sorry to use the whole answer - just for the sake of formatting :) Basically, the same answer from @tangens, but without a temporary file:

 echo "$(cat header.txt sample.txt)" > sample.txt 

Pay attention to the quotation marks ( " ) to preserve the end of the line, and the fact that cat is concatenated in the subprocess - so it should not be possible to overwrite the file ( sample.txt ) that is open for reading.

Hooray!

+8
source share
 cat header.txt sample.txt > temp.txt mv temp.txt sample.txt 
+4
source share

If you really want to do this 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 

However, I would use the cat method.

+1
source share

Why not do it the other way around, for example. add source files to your copyright file?

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

 for f in *.c do cp $f /tmp cat copyright.txt /tmp/$f >$f done 
0
source share

All Articles