Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by 510426762
510426762's Unix or Linux Image
510426762 510426762 is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 16 November 2017, 7:27 AM EST
Posts: 2
Thanks: 4
Thanked 0 Times in 0 Posts
How to insert something to a file from another file with shell?

complex.gro

Code:
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

Code:
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

Code:
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

Last edited by 510426762; 4 Weeks Ago at 01:58 PM.. Reason: Removed external links
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by 510426762
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 13 December 2017, 10:16 AM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
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 [code] and [/code] 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
The Following User Says Thank You to Corona688 For This Useful Post:
510426762 (4 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by 510426762
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 13 December 2017, 1:14 PM EST
Location: Galactic Empire
Posts: 3,612
Thanks: 254
Thanked 1,317 Times in 1,234 Posts
Here is an approach using awk:-

Code:
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

The Following User Says Thank You to Yoda For This Useful Post:
510426762 (4 Weeks Ago)
    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by 510426762
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 13 December 2017, 1:31 PM EST
Location: Amsterdam
Posts: 11,649
Thanks: 518
Thanked 3,385 Times in 2,984 Posts
Try:


Code:
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.
The Following User Says Thank You to Scrutinizer For This Useful Post:
510426762 (3 Weeks Ago)
Sponsored Links
    #5  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by 510426762
rovf's Unix or Linux Image
rovf rovf is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 13 December 2017, 3:28 AM EST
Posts: 186
Thanks: 26
Thanked 40 Times in 35 Posts

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

The Following User Says Thank You to rovf For This Useful Post:
510426762 (3 Weeks Ago)
Sponsored Links
    #6  
Old Unix and Linux 3 Weeks Ago   -   Original Discussion by 510426762
510426762's Unix or Linux Image
510426762 510426762 is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 16 November 2017, 7:27 AM EST
Posts: 2
Thanks: 4
Thanked 0 Times in 0 Posts
so tidy!!!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert text into a file using shell script karthick nath UNIX for Dummies Questions & Answers 5 01-08-2014 09:54 AM
Insert shell command into first line of output file lewk Shell Programming and Scripting 3 02-13-2012 02:39 AM
Shell script find word from one file and insert in another file sunilkumarsinha UNIX for Dummies Questions & Answers 2 08-25-2011 03:55 AM
Insert a function in a jsp file using Shell scripting 20033716 Shell Programming and Scripting 3 06-08-2009 03:44 AM
script to run shell command and insert results to existing xml file littlejon Shell Programming and Scripting 5 08-12-2005 05:59 PM



All times are GMT -4. The time now is 02:35 PM.