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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -