How to insert something to a file from another file with shell?

complex.gro

Great Red Owns Many ACres of Sand 
 6340
    6SER      N    1   2.724  13.826   0.957
    6SER     H1    2   2.685  13.863   1.041
    6SER     H2    3   2.783  13.748   0.979
   .
   .
   .
  405TYR      C 6338   4.079  11.113   0.908
  405TYR    OC1 6339   4.166  11.065   0.896
  405TYR    OC2 6340   4.045  11.204   0.829
   8.10576   5.73277   5.32581

lig.gro

MOL_GMX.gro created by acpype (Rev: 10101) on Tue Nov 14 21:43:44 2017
 37
    1  MOL   C1    1   5.094  10.087   2.868
    1  MOL   C2    2   4.998  10.172   2.807
    1  MOL   C3    3   4.940  10.136   2.690
   .
   .
   .
    1  MOL  H15   35   5.432  10.090   3.227
    1  MOL  H16   36   5.389  10.124   2.993
    1  MOL  H17   37   5.482   9.982   2.948
   15.26200    11.53200    18.72600

I want insert the red part of lig.gro in the complex.gro before the last line
and
change the 2nd line number in complex.gro(6340+37)

the result is like next
complex.gro

Great Red Owns Many ACres of Sand 
 6377
    6SER      N    1   2.724  13.826   0.957
    6SER     H1    2   2.685  13.863   1.041
    6SER     H2    3   2.783  13.748   0.979
   .
   .
   .
  405TYR      C 6338   4.079  11.113   0.908
  405TYR    OC1 6339   4.166  11.065   0.896
  405TYR    OC2 6340   4.045  11.204   0.829
    1  MOL   C1    1   5.094  10.087   2.868
    1  MOL   C2    2   4.998  10.172   2.807
    1  MOL   C3    3   4.940  10.136   2.690
   .
   .
   .
    1  MOL  H15   35   5.432  10.090   3.227
    1  MOL  H16   36   5.389  10.124   2.993
    1  MOL  H17   37   5.482   9.982   2.948
   8.10576   5.73277   5.32581

How can I reach the result by using shell script

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

1 Like

Here is an approach using awk:-

awk '
        NR == FNR {
                if ( FNR == 2 )
                        lig_atoms = $1
                if ( FNR > 2 )
                        atom[++i] = $0
                next
        }
        {
                if ( FNR == 2 )
                {
                        com_atoms = $1
                        $0 = com_atoms + lig_atoms
                }
                if ( FNR > 2 )
                        ++j
                if ( j > com_atoms )
                {
                        for ( c = 1; c < i; c++ )
                                print atom[c]
                }
                print
        }
' lig.gro complex.gro
1 Like

Try:

awk '
  NR==FNR {
    if(FNR==2)
      ligval=$1
    if(FNR>2 && NF>3)
      lig=lig $0 RS
    next
  }
  {
    if(FNR==2)
      $0=$1+ligval
    if(NF==3)
      $0=lig $0
    print
  }
'  lig.gro complex.gro

Assuming that the last line of a file has 3 fields.

1 Like
{ head -n -1 complex.gro; cat lig.gro; tail -n 1 complex.gro; } >combined.gro
1 Like

so tidy!!!