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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert something to a file from another file with shell?
# 1  
Old 11-14-2017
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; 11-14-2017 at 01:58 PM.. Reason: Removed external links
# 2  
Old 11-14-2017
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
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-14-2017
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

This User Gave Thanks to Yoda For This Post:
# 4  
Old 11-14-2017
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.
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-15-2017
Code:
{ head -n -1 complex.gro; cat lig.gro; tail -n 1 complex.gro; } >combined.gro

This User Gave Thanks to rovf For This Post:
# 6  
Old 11-16-2017
so tidy!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. Shell Programming and Scripting

Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem. I have three files like this.. cat file1.txt ======= unix is best unix is best linux is best unix is best linux is best linux is best unix is best unix is best cat file2.txt ======== Windows performs better Mac OS performs better Windows... (4 Replies)
Discussion started by: Jagadeesh Kumar
4 Replies

3. UNIX for Dummies Questions & Answers

Insert text into a file using shell script

Hi, I need to insert "Hello World" text into a file called hai.txt using shell scripting. Kindly help me. For eg: If I open the file hai.txt by giving linux command cat hai.txt, the content of the file should have the text Hello World in it. Thanks (5 Replies)
Discussion started by: karthick nath
5 Replies

4. Shell Programming and Scripting

Insert shell command into first line of output file

How can I insert the command executed on the shell into the first line of my output file? For example if I execute; zcat *.gz |grep “User5501” > users.out How can I make my users.out look like; zcat *.gz |grep “User5501” > users.out User5501 PA User5501 UA User5501 ZA... (3 Replies)
Discussion started by: lewk
3 Replies

5. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

6. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

7. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

8. Shell Programming and Scripting

Insert a function in a jsp file using Shell scripting

Greetings to all.I am new to the forum as well as to UNIX as well.I have a jsp file which has the following selectedStartMonth = request.getParameter( "startMonth" ); selectedStartDay = request.getParameter( "startDay" ); selectedStartYear = request.getParameter( "startYear" );... (3 Replies)
Discussion started by: 20033716
3 Replies

9. Shell Programming and Scripting

script to run shell command and insert results to existing xml file

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will 1. execute 'df -k' and return the volume names with specific text 2. surround each line of the above results in opening and closing xml tags 3. insert the results of step... (5 Replies)
Discussion started by: littlejon
5 Replies
Login or Register to Ask a Question