sedcommand-substitution

sed find and replace from line number


I need to copy line 3 and later from file a to file b (starting from line 3)

File a.py

import a

print('file a')

Original File b.py

import b

print('file b')

After copy

import b

print('file a')

I tried to use sed -n 3r<(sed -n '3,$p' a.py) b.py, but it inserts the lines instead of replacing them.


Solution

  • Using sed

    $ sed "3c $(sed -n '3p' a.py)" b.py
    import b
    
    print('file a')
    

    Use command substitution to allow another sed command to be run as an arguments and c to replace the contents of line 3.