How to prepend/(insert a string) in a file at arbitrary line numbers to multiple files in a directory?
Appending can be done using tee command.
cat file | tee -开发者_如何学Ca >> *
Is there a way to do a prepend/insertion? Thanks.
Using sed might help
example:
sed -i.bak '3 r tmp1.txt' settings.xml
will add the contents of tmp1.txt after line 3 in settings.xml (and create a backup file with the .bak extension)
Just a brief example: say, comment out specific/particular/arbitrary C lines:
$ echo -e "1\n2\n3\n4\n5\n6\n" | sed "3s,^,/* ,;5s,$, */,"
1
2
/* 3
4
5 */
6
Note:
- single
sed
command follows a format"${linenum}s/${search}/${replace}/"
- then two commands can be delimited by a semicolon '
;
' - using a comma '
,
' as delimiter, for easier reading ofs///
- Caret '
^
' matches start of line; dollar '$
' matches end of line; thes///
will replace only those (meta?)"characters"
Of course, then this should be modified with the -i
switch to sed
, to eventually replace file contents..
Cheers!
EDIT: Refs:
- Can we give multiple patterns to a sed command??? - The UNIX and Linux Forums
- Cute ‘sed’ Tricks to Modify Specific Lines Within File | The Linux Daily
精彩评论