Two-file processing in awk












1















I have file :



result.txt



Apple fruits 10 20 30
Car vehicle 40 50 60
Book study 70 80 90


Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max. I have another file config.txt which contains a property for each feature i.e.



config.txt



fruits Max
vehicle Median
study Min


So I want to write a script which shows only that column number for the feature which is defined in config.txt file.



Expected output:



Apple fruits 30
Car vehicle 50
Book study 70


I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:



awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt


I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.










share|improve this question



























    1















    I have file :



    result.txt



    Apple fruits 10 20 30
    Car vehicle 40 50 60
    Book study 70 80 90


    Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max. I have another file config.txt which contains a property for each feature i.e.



    config.txt



    fruits Max
    vehicle Median
    study Min


    So I want to write a script which shows only that column number for the feature which is defined in config.txt file.



    Expected output:



    Apple fruits 30
    Car vehicle 50
    Book study 70


    I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:



    awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt


    I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.










    share|improve this question

























      1












      1








      1


      1






      I have file :



      result.txt



      Apple fruits 10 20 30
      Car vehicle 40 50 60
      Book study 70 80 90


      Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max. I have another file config.txt which contains a property for each feature i.e.



      config.txt



      fruits Max
      vehicle Median
      study Min


      So I want to write a script which shows only that column number for the feature which is defined in config.txt file.



      Expected output:



      Apple fruits 30
      Car vehicle 50
      Book study 70


      I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:



      awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt


      I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.










      share|improve this question














      I have file :



      result.txt



      Apple fruits 10 20 30
      Car vehicle 40 50 60
      Book study 70 80 90


      Assume here 2nd column is a feature, 3rd column is Min, 4th column is Median, 5th column is Max. I have another file config.txt which contains a property for each feature i.e.



      config.txt



      fruits Max
      vehicle Median
      study Min


      So I want to write a script which shows only that column number for the feature which is defined in config.txt file.



      Expected output:



      Apple fruits 30
      Car vehicle 50
      Book study 70


      I am following this link https://stackoverflow.com/a/40206489/10220825. Here what I have tried:



      awk 'FNR==NR{arr[$2];next} $1 in arr {var =$2;print var}' result.txt config.txt


      I can able to hold the property(like Min, Max, Median) in a variable for the corresponding feature defined in result.txt, but not able to display the column for that variable. Please suggest me an approach on how to print the column for the corresponding column.







      bash scripts awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 19 at 11:37









      AryaArya

      184




      184






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to map the feature names in the config.txt file to field indices in the result.txt file, e.g.



          awk '
          BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
          NR==FNR {arr[$1] = ind[$2]; next}
          $2 in arr {print $1,$2,$(arr[$2])}
          ' config.txt result.txt
          Apple fruits 30
          Car vehicle 50
          Book study 70





          share|improve this answer























            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%2f1119500%2ftwo-file-processing-in-awk%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














            You need to map the feature names in the config.txt file to field indices in the result.txt file, e.g.



            awk '
            BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
            NR==FNR {arr[$1] = ind[$2]; next}
            $2 in arr {print $1,$2,$(arr[$2])}
            ' config.txt result.txt
            Apple fruits 30
            Car vehicle 50
            Book study 70





            share|improve this answer




























              0














              You need to map the feature names in the config.txt file to field indices in the result.txt file, e.g.



              awk '
              BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
              NR==FNR {arr[$1] = ind[$2]; next}
              $2 in arr {print $1,$2,$(arr[$2])}
              ' config.txt result.txt
              Apple fruits 30
              Car vehicle 50
              Book study 70





              share|improve this answer


























                0












                0








                0







                You need to map the feature names in the config.txt file to field indices in the result.txt file, e.g.



                awk '
                BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
                NR==FNR {arr[$1] = ind[$2]; next}
                $2 in arr {print $1,$2,$(arr[$2])}
                ' config.txt result.txt
                Apple fruits 30
                Car vehicle 50
                Book study 70





                share|improve this answer













                You need to map the feature names in the config.txt file to field indices in the result.txt file, e.g.



                awk '
                BEGIN {ind["Min"]=3; ind["Median"]=4; ind["Max"]=5}
                NR==FNR {arr[$1] = ind[$2]; next}
                $2 in arr {print $1,$2,$(arr[$2])}
                ' config.txt result.txt
                Apple fruits 30
                Car vehicle 50
                Book study 70






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 19 at 13:06









                steeldriversteeldriver

                69.6k11114186




                69.6k11114186






























                    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%2f1119500%2ftwo-file-processing-in-awk%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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                    Eduardo VII do Reino Unido