Replace column between patterns in one file from another file












0















I am trying to write shell script which reads file1, get a specific column from this file. And replace column in file2 with the column extracted from file1 between two patterns .



File1



Line1
Line2
.
LineN
ATOM C1 C2 C3
ATOM P23 HI IKJ
ATOM S23 JSK SN
BOND
Many lines
END


File2



Few Lines
Pattern1
1 C -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 C -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 N -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I want to get the column $2 from file 1 and replace it the the column $2 in file2 between pattern1 and pattern 2.



Output



Few Lines
Pattern1
1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I have tried few things so far.



awk '($1=="ATOM") {print $2}' file1
awk '/pattern1/{flag=1; next} /pattern2/{flag=0} flag' file2


I can store the column 2 in file1. Also, lines between both the patterns from file2.



I am sure with something like FNR=NR, I should be able to handle both files at the same time. Any help would be great to proceed further.










share|improve this question























  • This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

    – RavinderSingh13
    Nov 21 '18 at 19:26
















0















I am trying to write shell script which reads file1, get a specific column from this file. And replace column in file2 with the column extracted from file1 between two patterns .



File1



Line1
Line2
.
LineN
ATOM C1 C2 C3
ATOM P23 HI IKJ
ATOM S23 JSK SN
BOND
Many lines
END


File2



Few Lines
Pattern1
1 C -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 C -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 N -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I want to get the column $2 from file 1 and replace it the the column $2 in file2 between pattern1 and pattern 2.



Output



Few Lines
Pattern1
1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I have tried few things so far.



awk '($1=="ATOM") {print $2}' file1
awk '/pattern1/{flag=1; next} /pattern2/{flag=0} flag' file2


I can store the column 2 in file1. Also, lines between both the patterns from file2.



I am sure with something like FNR=NR, I should be able to handle both files at the same time. Any help would be great to proceed further.










share|improve this question























  • This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

    – RavinderSingh13
    Nov 21 '18 at 19:26














0












0








0








I am trying to write shell script which reads file1, get a specific column from this file. And replace column in file2 with the column extracted from file1 between two patterns .



File1



Line1
Line2
.
LineN
ATOM C1 C2 C3
ATOM P23 HI IKJ
ATOM S23 JSK SN
BOND
Many lines
END


File2



Few Lines
Pattern1
1 C -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 C -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 N -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I want to get the column $2 from file 1 and replace it the the column $2 in file2 between pattern1 and pattern 2.



Output



Few Lines
Pattern1
1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I have tried few things so far.



awk '($1=="ATOM") {print $2}' file1
awk '/pattern1/{flag=1; next} /pattern2/{flag=0} flag' file2


I can store the column 2 in file1. Also, lines between both the patterns from file2.



I am sure with something like FNR=NR, I should be able to handle both files at the same time. Any help would be great to proceed further.










share|improve this question














I am trying to write shell script which reads file1, get a specific column from this file. And replace column in file2 with the column extracted from file1 between two patterns .



File1



Line1
Line2
.
LineN
ATOM C1 C2 C3
ATOM P23 HI IKJ
ATOM S23 JSK SN
BOND
Many lines
END


File2



Few Lines
Pattern1
1 C -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 C -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 N -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I want to get the column $2 from file 1 and replace it the the column $2 in file2 between pattern1 and pattern 2.



Output



Few Lines
Pattern1
1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


I have tried few things so far.



awk '($1=="ATOM") {print $2}' file1
awk '/pattern1/{flag=1; next} /pattern2/{flag=0} flag' file2


I can store the column 2 in file1. Also, lines between both the patterns from file2.



I am sure with something like FNR=NR, I should be able to handle both files at the same time. Any help would be great to proceed further.







awk






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 19:21









dipak sanapdipak sanap

11




11













  • This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

    – RavinderSingh13
    Nov 21 '18 at 19:26



















  • This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

    – RavinderSingh13
    Nov 21 '18 at 19:26

















This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

– RavinderSingh13
Nov 21 '18 at 19:26





This thread looks like stackoverflow.com/questions/53417240/… so please close another one then if both are same.

– RavinderSingh13
Nov 21 '18 at 19:26












1 Answer
1






active

oldest

votes


















0














Per Your question and as You mentioning (FNR==NR) solution You can use this AWK:



awk '
( FNR==NR && /^ATOM / ) { atoms[++atomn]=$2; }
( FNR!=NR && /^Patttern2$/ ) { doreplace=0; }
doreplace { $2=atoms[++atomn]; }
( FNR!=NR && /^Pattern1$/ ) { doreplace=1; atomn=0; }
FNR!=NR
' file1 file2


I rather prefer this way when dealing with metafile and datafiles:



awk '
BEGIN {
if ( ARGC != 3 ) exit(1);
while ( getline < ARGV[2] ) if ( $0 ~ /^ATOM / ) atoms[++atomn]=$2;
ARGC=2;
}
/^Patttern2$/ { doreplace=0; }
doreplace { $2=atoms[++atomn]; }
/^Pattern1$/ { doreplace=1; atomn=0; }
1
' file2 file1


Both will work on your provided inputs. Here Output:



Few Lines
Pattern1
1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
Patttern2
Lines


Both assumes that in metafile (file1) and datafile (file2) you have exactly same number of ATOM and lines between patterns. If You are not able to validate this I would add logic to watch this not to iterate through in-allocated array elements. However in AWK nothing will happen just replacement will be done with empty string instead.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419179%2freplace-column-between-patterns-in-one-file-from-another-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Per Your question and as You mentioning (FNR==NR) solution You can use this AWK:



    awk '
    ( FNR==NR && /^ATOM / ) { atoms[++atomn]=$2; }
    ( FNR!=NR && /^Patttern2$/ ) { doreplace=0; }
    doreplace { $2=atoms[++atomn]; }
    ( FNR!=NR && /^Pattern1$/ ) { doreplace=1; atomn=0; }
    FNR!=NR
    ' file1 file2


    I rather prefer this way when dealing with metafile and datafiles:



    awk '
    BEGIN {
    if ( ARGC != 3 ) exit(1);
    while ( getline < ARGV[2] ) if ( $0 ~ /^ATOM / ) atoms[++atomn]=$2;
    ARGC=2;
    }
    /^Patttern2$/ { doreplace=0; }
    doreplace { $2=atoms[++atomn]; }
    /^Pattern1$/ { doreplace=1; atomn=0; }
    1
    ' file2 file1


    Both will work on your provided inputs. Here Output:



    Few Lines
    Pattern1
    1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
    2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
    3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
    Patttern2
    Lines


    Both assumes that in metafile (file1) and datafile (file2) you have exactly same number of ATOM and lines between patterns. If You are not able to validate this I would add logic to watch this not to iterate through in-allocated array elements. However in AWK nothing will happen just replacement will be done with empty string instead.






    share|improve this answer




























      0














      Per Your question and as You mentioning (FNR==NR) solution You can use this AWK:



      awk '
      ( FNR==NR && /^ATOM / ) { atoms[++atomn]=$2; }
      ( FNR!=NR && /^Patttern2$/ ) { doreplace=0; }
      doreplace { $2=atoms[++atomn]; }
      ( FNR!=NR && /^Pattern1$/ ) { doreplace=1; atomn=0; }
      FNR!=NR
      ' file1 file2


      I rather prefer this way when dealing with metafile and datafiles:



      awk '
      BEGIN {
      if ( ARGC != 3 ) exit(1);
      while ( getline < ARGV[2] ) if ( $0 ~ /^ATOM / ) atoms[++atomn]=$2;
      ARGC=2;
      }
      /^Patttern2$/ { doreplace=0; }
      doreplace { $2=atoms[++atomn]; }
      /^Pattern1$/ { doreplace=1; atomn=0; }
      1
      ' file2 file1


      Both will work on your provided inputs. Here Output:



      Few Lines
      Pattern1
      1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
      2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
      3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
      Patttern2
      Lines


      Both assumes that in metafile (file1) and datafile (file2) you have exactly same number of ATOM and lines between patterns. If You are not able to validate this I would add logic to watch this not to iterate through in-allocated array elements. However in AWK nothing will happen just replacement will be done with empty string instead.






      share|improve this answer


























        0












        0








        0







        Per Your question and as You mentioning (FNR==NR) solution You can use this AWK:



        awk '
        ( FNR==NR && /^ATOM / ) { atoms[++atomn]=$2; }
        ( FNR!=NR && /^Patttern2$/ ) { doreplace=0; }
        doreplace { $2=atoms[++atomn]; }
        ( FNR!=NR && /^Pattern1$/ ) { doreplace=1; atomn=0; }
        FNR!=NR
        ' file1 file2


        I rather prefer this way when dealing with metafile and datafiles:



        awk '
        BEGIN {
        if ( ARGC != 3 ) exit(1);
        while ( getline < ARGV[2] ) if ( $0 ~ /^ATOM / ) atoms[++atomn]=$2;
        ARGC=2;
        }
        /^Patttern2$/ { doreplace=0; }
        doreplace { $2=atoms[++atomn]; }
        /^Pattern1$/ { doreplace=1; atomn=0; }
        1
        ' file2 file1


        Both will work on your provided inputs. Here Output:



        Few Lines
        Pattern1
        1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
        2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
        3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
        Patttern2
        Lines


        Both assumes that in metafile (file1) and datafile (file2) you have exactly same number of ATOM and lines between patterns. If You are not able to validate this I would add logic to watch this not to iterate through in-allocated array elements. However in AWK nothing will happen just replacement will be done with empty string instead.






        share|improve this answer













        Per Your question and as You mentioning (FNR==NR) solution You can use this AWK:



        awk '
        ( FNR==NR && /^ATOM / ) { atoms[++atomn]=$2; }
        ( FNR!=NR && /^Patttern2$/ ) { doreplace=0; }
        doreplace { $2=atoms[++atomn]; }
        ( FNR!=NR && /^Pattern1$/ ) { doreplace=1; atomn=0; }
        FNR!=NR
        ' file1 file2


        I rather prefer this way when dealing with metafile and datafiles:



        awk '
        BEGIN {
        if ( ARGC != 3 ) exit(1);
        while ( getline < ARGV[2] ) if ( $0 ~ /^ATOM / ) atoms[++atomn]=$2;
        ARGC=2;
        }
        /^Patttern2$/ { doreplace=0; }
        doreplace { $2=atoms[++atomn]; }
        /^Pattern1$/ { doreplace=1; atomn=0; }
        1
        ' file2 file1


        Both will work on your provided inputs. Here Output:



        Few Lines
        Pattern1
        1 C1 -9.2429 -1.3783 -9.5091 C.3 1 LIG1 0.0555
        2 P23 -10.5865 -0.8658 -8.9679 C.3 1 LIG1 0.0529
        3 S23 -11.3072 -0.5779 -10.1774 N.am 1 LIG1 -0.2940
        Patttern2
        Lines


        Both assumes that in metafile (file1) and datafile (file2) you have exactly same number of ATOM and lines between patterns. If You are not able to validate this I would add logic to watch this not to iterate through in-allocated array elements. However in AWK nothing will happen just replacement will be done with empty string instead.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:44









        KubatorKubator

        74911




        74911
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419179%2freplace-column-between-patterns-in-one-file-from-another-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$