I have been trying to insert a file as the first line of another with following SED command, without much success. Each time the file is inserted after line 1. Is there a switch that will inserted before line 1?sed -i '1r file1.txt' file2.txtThanks in advance.
SED - Insert file at top of another
sed
The sed command r will place the contents of the specified file in the output before reading the next line of input. Unfortunately you can't specify 0 as the address for this command, so there's no way to insert the contents of a file before the first line of input (without poking around with the hold space).You could, however, just use plain old cat. It is, after all, the command for concatenating files:$ cat file1.txt file2.txt >out && mv out file2.txtTo be sure you're writing to a temporary file that does not already exist, one may use the mktemp utility:$ tmp=$(mktemp) && cat file1.txt file2.txt >$tmp && mv $tmp file2.txtThis is slightly awkward on the command line, but a good precaution in any script that needs to write to a temporary file.