shell - Bash : How to insert a block of text after a certain line into a file in Unix? -
i have file file1.txt below :
line 1 file 1 line 2 file 1 line 3 file 1 line 4 file 1
and second file file2.txt below :
line 1 file 2 line 2 file 2 line 3 file 2 line 4 file 2 line 5 file 2
i want right 1 line command (preferably sed ) lines 2-4 second file (file2.txt) , add lines after line 3 first file(file1.txt) .
so output should below :
line 1 file 1 line 2 file 1 line 3 file 1 line 2 file 2 line 3 file 2 line 4 file 2 line 4 file 1
for getting lines 2-4 second file use :
sed -n '2,4p' file2.txt
and inserting lines after line 3 in file 1 use below :
sed '4i<what insert here>' file1.txt
but how combine both these operations ?
agreeing other answerer job awk
, can done sed
.
sed
offers kind of strange commands besides famous s
command , there e
execute command gnu sed. after have given pieces in question, here how combine ideas:
sed '4 e sed -n 2,4p file2.txt' file1.txt
Comments
Post a Comment