Add text to file at certain line in Linux [duplicate]

I want to add a specific line, lets say avatar to the files that starts with MakeFile and avatar should be added to the 15th line in the file.

This is how to add text to files:

echo 'avatar' >> MakeFile.websvc

and this is how to add text to files that starts with MakeFile I think:

echo 'avatar' >> *MakeFile.

But I can not manage to add this line to the 15th line of the file.


Solution 1:

You can use sed to solve this:

sed "15i avatar" Makefile.txt

or use the -i option to save the changes made to the file.

sed -i "15i avatar" Makefile.txt

To change all the files beginning that start Makefile:

sed "15i avatar" Makefile*

Note: In the above 15 is your line of interest to place the text.

Solution 2:

Using sed :

sed -i '15i\avatar\' Makefile*

where the -i option indicates that transformation occurs in-place (which is useful for instance when you want to process several files).

Also, in your question, *MakeFile stands for 'all files that end with MakeFile', while 'all files that begin with MakeFile' would be denoted as MakeFile*.