LookIntoEast
LookIntoEast

Reputation: 8798

Insert a file at the beginning of a file possibly using sed

I think we already have similar post using sed to add "text" at the beginning of a file Say: sed -i '1i text' inputfile

But here my question is: my text has many lines, so I put them in a file (file1). And I hope to insert the content in file1 at the beginning of file2.

How can I do that using sed, or other approaches? thx

edit:

Sorry I'm myself complicating this question! This is an idiot question because we can simply do by "cat"! :) I'm an idiot

Upvotes: 0

Views: 141

Answers (3)

potong
potong

Reputation: 58371

This might work for you (as an exercise as cat is the obvious choice):

sed '1{h;r file1'$'\n'';d};2{H;g}' file2

Upvotes: 0

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

How about doing

cat file1 file2

(Well, this is not "inplace" editing, though, you probably need to use a temp file or a buffer.)

Notice that in some shells, you will also be able to do

command < file1 < file2

Upvotes: 2

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

Using awk:

awk 'BEGIN { while ((getline tmp < "TEMPLATE" ) > 0) { print tmp } 
             close("TEMPLATE")}
     { print }' ORIGFILE > NEWFILE && mv NEWFILE ORIGFILE

Using vim:

vim -c "read TEMPLATE" -c "read FILE" -c "wq"

Upvotes: 0

Related Questions