4.40. How do I read (insert/add) a file at the top of a textfile?
Normally, adding a "header" file to the top of a "body" file is
done from the command prompt before passing the file on to sed.
(MS-DOS below version 6.0 must use COPY and DEL instead of MOVE in
the following example.)
copy header.txt+body temp # MS-DOS command 1
echo Y | move temp body # MS-DOS command 2
#
cat header.txt body >temp; mv temp body # Unix commands
However, if inserting the file must occur within sed, there is a
way. The sed command "1 r header.txt" will not work; it will print
line 1 and then insert "header.txt" between lines 1 and 2. The
following script solves this problem; however, there must be at
least 2 lines in the target file for the script to work properly.
# sed script to insert "header.txt" above the first line
1{h; r header.txt
D; }
2{x; G; }
#---end of sed script---