Replace same line in file 1 from file 2 with conditon

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












File 1:



19a9s
c9019
5777


File 2:



99a9s
89019
10919


Expected output



19a9s
89019
5777


So in file 1 on line#2 it's start with letter c. which i would like to use it as condition if the line start with letter c so i want to replace it from the second file which is file 2



I Tried with the following but failed to give me the expected output:



awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning

/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element

1 #alias for `print $0`
' file2 file1









share|improve this question























  • @don_crissti i don't know from where to start :(. sorry
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 19:44











  • @don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
    – mosvy
    Nov 17 at 19:53










  • @mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
    – don_crissti
    Nov 17 at 20:03










  • @don_crissti i edited the code but the same :S
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:43










  • @don_crissti same !
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:47














up vote
0
down vote

favorite












File 1:



19a9s
c9019
5777


File 2:



99a9s
89019
10919


Expected output



19a9s
89019
5777


So in file 1 on line#2 it's start with letter c. which i would like to use it as condition if the line start with letter c so i want to replace it from the second file which is file 2



I Tried with the following but failed to give me the expected output:



awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning

/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element

1 #alias for `print $0`
' file2 file1









share|improve this question























  • @don_crissti i don't know from where to start :(. sorry
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 19:44











  • @don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
    – mosvy
    Nov 17 at 19:53










  • @mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
    – don_crissti
    Nov 17 at 20:03










  • @don_crissti i edited the code but the same :S
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:43










  • @don_crissti same !
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:47












up vote
0
down vote

favorite









up vote
0
down vote

favorite











File 1:



19a9s
c9019
5777


File 2:



99a9s
89019
10919


Expected output



19a9s
89019
5777


So in file 1 on line#2 it's start with letter c. which i would like to use it as condition if the line start with letter c so i want to replace it from the second file which is file 2



I Tried with the following but failed to give me the expected output:



awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning

/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element

1 #alias for `print $0`
' file2 file1









share|improve this question















File 1:



19a9s
c9019
5777


File 2:



99a9s
89019
10919


Expected output



19a9s
89019
5777


So in file 1 on line#2 it's start with letter c. which i would like to use it as condition if the line start with letter c so i want to replace it from the second file which is file 2



I Tried with the following but failed to give me the expected output:



awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning

/^c/$0=S[FNR] #for line stared with `c`
$0=S[++count] #replace line by corresponded array element

1 #alias for `print $0`
' file2 file1






awk sed regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:47

























asked Nov 17 at 19:37









αԋɱҽԃ αмєяιcαη

439421




439421











  • @don_crissti i don't know from where to start :(. sorry
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 19:44











  • @don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
    – mosvy
    Nov 17 at 19:53










  • @mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
    – don_crissti
    Nov 17 at 20:03










  • @don_crissti i edited the code but the same :S
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:43










  • @don_crissti same !
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:47
















  • @don_crissti i don't know from where to start :(. sorry
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 19:44











  • @don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
    – mosvy
    Nov 17 at 19:53










  • @mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
    – don_crissti
    Nov 17 at 20:03










  • @don_crissti i edited the code but the same :S
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:43










  • @don_crissti same !
    – αԋɱҽԃ αмєяιcαη
    Nov 17 at 20:47















@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44





@don_crissti i don't know from where to start :(. sorry
– αԋɱҽԃ αмєяιcαη
Nov 17 at 19:44













@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
– mosvy
Nov 17 at 19:53




@don_crissti sorry for touting my own garbage, but I don't think it's advisable and/or necessary to save an entire file in an array. awk is perfectly able to process two files in parallel, line by line
– mosvy
Nov 17 at 19:53












@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03




@mosvy - no problem, while you're right I think the array method is easier to digest for someone not knowing awk but wanting to start learning.
– don_crissti
Nov 17 at 20:03












@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43




@don_crissti i edited the code but the same :S
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:43












@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47




@don_crissti same !
– αԋɱҽԃ αмєяιcαη
Nov 17 at 20:47










2 Answers
2






active

oldest

votes

















up vote
1
down vote













awk '
NR == FNR #for lines in first file
S[NR] = $0 #put line in array `S` with row number as index
next #starts script from the beginning

/^c/$0=S[FNR] #for line stared with `c`

1 #alias for `print $0`
' file2 file1





share|improve this answer





























    up vote
    0
    down vote













    How far would



    paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
    19a9s
    89019
    5777


    get you?






    share|improve this answer




















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      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',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f482386%2freplace-same-line-in-file-1-from-file-2-with-conditon%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      awk '
      NR == FNR #for lines in first file
      S[NR] = $0 #put line in array `S` with row number as index
      next #starts script from the beginning

      /^c/$0=S[FNR] #for line stared with `c`

      1 #alias for `print $0`
      ' file2 file1





      share|improve this answer


























        up vote
        1
        down vote













        awk '
        NR == FNR #for lines in first file
        S[NR] = $0 #put line in array `S` with row number as index
        next #starts script from the beginning

        /^c/$0=S[FNR] #for line stared with `c`

        1 #alias for `print $0`
        ' file2 file1





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          awk '
          NR == FNR #for lines in first file
          S[NR] = $0 #put line in array `S` with row number as index
          next #starts script from the beginning

          /^c/$0=S[FNR] #for line stared with `c`

          1 #alias for `print $0`
          ' file2 file1





          share|improve this answer














          awk '
          NR == FNR #for lines in first file
          S[NR] = $0 #put line in array `S` with row number as index
          next #starts script from the beginning

          /^c/$0=S[FNR] #for line stared with `c`

          1 #alias for `print $0`
          ' file2 file1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 17 at 20:59

























          answered Nov 17 at 20:09









          αԋɱҽԃ αмєяιcαη

          439421




          439421






















              up vote
              0
              down vote













              How far would



              paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
              19a9s
              89019
              5777


              get you?






              share|improve this answer
























                up vote
                0
                down vote













                How far would



                paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
                19a9s
                89019
                5777


                get you?






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  How far would



                  paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
                  19a9s
                  89019
                  5777


                  get you?






                  share|improve this answer












                  How far would



                  paste file[12] | sed 's/^c[^ ]* //; t; s/ .*$//;'
                  19a9s
                  89019
                  5777


                  get you?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 22:24









                  RudiC

                  3,0911211




                  3,0911211



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482386%2freplace-same-line-in-file-1-from-file-2-with-conditon%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

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay