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.
You can use
sed
to solve this:or use the
-i
option to save the changes made to the file.To change all the files beginning that start
Makefile
:Note: In the above
15
is your line of interest to place the text.Using sed :
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*.
If you want a more portable version, you can use
ex
, which will work on any *Nix system. (It's specified by POSIX.) The Sed commands given so far depend on GNU Sed.To insert a line containing nothing but "avatar" at the 15th line of each file in the current directory whose name begins with "Makefile.", use:
If you need to pass in the string and the line-number options to the script, try this:
-i
edit the input file, do not make a backup copy$.
is the line numberThis is based on my solution to Insert a line at specific line number with sed or awk, which contains several other methods of passing options to Perl, as well as explanations of the command line options.