Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

sed

insert

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.

like image 897
Sriram Avatar asked Sep 24 '10 06:09

Sriram


5 Answers

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.

like image 35
Dennis Williamson Avatar answered Oct 23 '22 02:10

Dennis Williamson


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

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

Note the quotes (") to preserve the line endings, and the fact that the cat concatenation runs in a subprocess - so there shouldn't be the possibility of overwriting a file (the sample.txt) which is open for reading..

Cheers!

like image 184
sdaau Avatar answered Oct 23 '22 00:10

sdaau


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.

like image 44
hluk Avatar answered Oct 23 '22 01:10

hluk


cat header.txt sample.txt > temp.txt
mv temp.txt sample.txt
like image 5
tangens Avatar answered Oct 23 '22 01:10

tangens


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.

like image 1
codaddict Avatar answered Oct 23 '22 02:10

codaddict