awk replace value in colunm p row i by value in same column next row












0















#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, , price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL


I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:



#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL


I cannot find how to reference the next line in awk:



cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'









share|improve this question





























    0















    #!/usr/bin/env bash
    cat > example_file.txt <<EOL
    group, , price
    1, value, 3.21
    1, 3.42, 4.11
    1, 3.5, 1.22
    2, 4.1, 9.2
    2, 4.2, 2.11
    EOL


    I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:



    #!/usr/bin/env bash
    cat > example_file.txt <<EOL
    group, value, price
    1, value, 3.21
    1, 3.42, 4.11
    1, 3.5, 1.22
    2, 4.1, 9.2
    2, 4.2, 2.11
    EOL


    I cannot find how to reference the next line in awk:



    cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'









    share|improve this question



























      0












      0








      0








      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      group, , price
      1, value, 3.21
      1, 3.42, 4.11
      1, 3.5, 1.22
      2, 4.1, 9.2
      2, 4.2, 2.11
      EOL


      I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:



      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      group, value, price
      1, value, 3.21
      1, 3.42, 4.11
      1, 3.5, 1.22
      2, 4.1, 9.2
      2, 4.2, 2.11
      EOL


      I cannot find how to reference the next line in awk:



      cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'









      share|improve this question
















      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      group, , price
      1, value, 3.21
      1, 3.42, 4.11
      1, 3.5, 1.22
      2, 4.1, 9.2
      2, 4.2, 2.11
      EOL


      I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:



      #!/usr/bin/env bash
      cat > example_file.txt <<EOL
      group, value, price
      1, value, 3.21
      1, 3.42, 4.11
      1, 3.5, 1.22
      2, 4.1, 9.2
      2, 4.2, 2.11
      EOL


      I cannot find how to reference the next line in awk:



      cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'






      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 hours ago







      user2413

















      asked 7 hours ago









      user2413user2413

      3,936133962




      3,936133962






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use getline to get the next line of input, and split to split it into an array of fields using the current FS:



          $ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt 
          group, value, price
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11




          If you want to print the "got" line as well, it's still in ln after splitting (to get the order right, we also need to explicitly print first, and skip the default print using next):



          $ awk -F, '
          BEGIN { OFS = FS }
          NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
          ' example_file.txt
          group, value, price
          1, value, 3.21
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11





          share|improve this answer


























          • sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

            – user2413
            7 hours ago











          • @user2413 please see amended answer

            – steeldriver
            7 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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%2faskubuntu.com%2fquestions%2f1109796%2fawk-replace-value-in-colunm-p-row-i-by-value-in-same-column-next-row%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









          1














          You can use getline to get the next line of input, and split to split it into an array of fields using the current FS:



          $ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt 
          group, value, price
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11




          If you want to print the "got" line as well, it's still in ln after splitting (to get the order right, we also need to explicitly print first, and skip the default print using next):



          $ awk -F, '
          BEGIN { OFS = FS }
          NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
          ' example_file.txt
          group, value, price
          1, value, 3.21
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11





          share|improve this answer


























          • sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

            – user2413
            7 hours ago











          • @user2413 please see amended answer

            – steeldriver
            7 hours ago
















          1














          You can use getline to get the next line of input, and split to split it into an array of fields using the current FS:



          $ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt 
          group, value, price
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11




          If you want to print the "got" line as well, it's still in ln after splitting (to get the order right, we also need to explicitly print first, and skip the default print using next):



          $ awk -F, '
          BEGIN { OFS = FS }
          NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
          ' example_file.txt
          group, value, price
          1, value, 3.21
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11





          share|improve this answer


























          • sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

            – user2413
            7 hours ago











          • @user2413 please see amended answer

            – steeldriver
            7 hours ago














          1












          1








          1







          You can use getline to get the next line of input, and split to split it into an array of fields using the current FS:



          $ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt 
          group, value, price
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11




          If you want to print the "got" line as well, it's still in ln after splitting (to get the order right, we also need to explicitly print first, and skip the default print using next):



          $ awk -F, '
          BEGIN { OFS = FS }
          NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
          ' example_file.txt
          group, value, price
          1, value, 3.21
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11





          share|improve this answer















          You can use getline to get the next line of input, and split to split it into an array of fields using the current FS:



          $ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt 
          group, value, price
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11




          If you want to print the "got" line as well, it's still in ln after splitting (to get the order right, we also need to explicitly print first, and skip the default print using next):



          $ awk -F, '
          BEGIN { OFS = FS }
          NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
          ' example_file.txt
          group, value, price
          1, value, 3.21
          1, 3.42, 4.11
          1, 3.5, 1.22
          2, 4.1, 9.2
          2, 4.2, 2.11






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 7 hours ago









          steeldriversteeldriver

          66.3k11106179




          66.3k11106179













          • sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

            – user2413
            7 hours ago











          • @user2413 please see amended answer

            – steeldriver
            7 hours ago



















          • sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

            – user2413
            7 hours ago











          • @user2413 please see amended answer

            – steeldriver
            7 hours ago

















          sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

          – user2413
          7 hours ago





          sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear

          – user2413
          7 hours ago













          @user2413 please see amended answer

          – steeldriver
          7 hours ago





          @user2413 please see amended answer

          – steeldriver
          7 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%2faskubuntu.com%2fquestions%2f1109796%2fawk-replace-value-in-colunm-p-row-i-by-value-in-same-column-next-row%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

          GameSpot

          connect to host localhost port 22: Connection refused

          Getting a Wifi WPA2 wifi connection