Search code examples
linuxlinescopying

use of sed and perl to do the word processing, copying lines from one file to another


I am new to Linux and have a challenging task.

I have 3 data files, and need to do the following:

  1. Go to line 31 of file 1, delete it
  2. Read 1 line from file 2 and add in place of deleted line
  3. Go to line 97 of file 1 delete it and then read the line 1 from file 2 and add in place of that deleted line in file 1.

The thing is also important to keep the same file i.e file , it is not to be changed.

I tried different versions of sed and perl, with buffer copying tricks but was not successful.

I am open for all suggestions and request the experts to give me suggestions.


Solution

  • I cannot find a reference to the 3rd file in your question, but if you mean replace line number 31 of file 1 with the 1st line of file 2, and replace line number 97 of file 1 with the 2nd line of file 2:

    sed -i -e '30R f2
    31d;96R f2
    97d' f1
    

    The new lines are important after f2 so sed knows that it is the end of the file name.

    Note that the R command is a GNU extension, it is not standard.