How to use awk build in variable in shell script












0















I need to use awk build in variable $NF with shell script. The input string will be something like this



Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD


I want to extract the last element alone from this string



NE40Ef-02-T1M1N2-HUW_S1_RD


In the server if I do something like this



cat aa |awk '{print $NF}'


I get the correct result. But when I try to do ssh and then try to execute the same it does not understand what is $NF.



I am trying to do something like this



cat aa |awk -v temp ="$NF" '{print $NF}'


But this does not work.










share|improve this question

























  • foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

    – Cyrus
    Jan 1 at 13:39













  • awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

    – Walter A
    Jan 1 at 13:41











  • temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

    – Walter A
    Jan 1 at 13:43


















0















I need to use awk build in variable $NF with shell script. The input string will be something like this



Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD


I want to extract the last element alone from this string



NE40Ef-02-T1M1N2-HUW_S1_RD


In the server if I do something like this



cat aa |awk '{print $NF}'


I get the correct result. But when I try to do ssh and then try to execute the same it does not understand what is $NF.



I am trying to do something like this



cat aa |awk -v temp ="$NF" '{print $NF}'


But this does not work.










share|improve this question

























  • foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

    – Cyrus
    Jan 1 at 13:39













  • awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

    – Walter A
    Jan 1 at 13:41











  • temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

    – Walter A
    Jan 1 at 13:43
















0












0








0








I need to use awk build in variable $NF with shell script. The input string will be something like this



Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD


I want to extract the last element alone from this string



NE40Ef-02-T1M1N2-HUW_S1_RD


In the server if I do something like this



cat aa |awk '{print $NF}'


I get the correct result. But when I try to do ssh and then try to execute the same it does not understand what is $NF.



I am trying to do something like this



cat aa |awk -v temp ="$NF" '{print $NF}'


But this does not work.










share|improve this question
















I need to use awk build in variable $NF with shell script. The input string will be something like this



Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD


I want to extract the last element alone from this string



NE40Ef-02-T1M1N2-HUW_S1_RD


In the server if I do something like this



cat aa |awk '{print $NF}'


I get the correct result. But when I try to do ssh and then try to execute the same it does not understand what is $NF.



I am trying to do something like this



cat aa |awk -v temp ="$NF" '{print $NF}'


But this does not work.







shell awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 13:35









Cyrus

46.2k43878




46.2k43878










asked Jan 1 at 12:14









user1107731user1107731

66117




66117













  • foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

    – Cyrus
    Jan 1 at 13:39













  • awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

    – Walter A
    Jan 1 at 13:41











  • temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

    – Walter A
    Jan 1 at 13:43





















  • foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

    – Cyrus
    Jan 1 at 13:39













  • awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

    – Walter A
    Jan 1 at 13:41











  • temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

    – Walter A
    Jan 1 at 13:43



















foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

– Cyrus
Jan 1 at 13:39







foo=($(cat aa)); temp="${foo[${#foo[@]}-1]}"?

– Cyrus
Jan 1 at 13:39















awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

– Walter A
Jan 1 at 13:41





awk -v temp ="$NF" will assign the shell variable NF to a new awk variable temp that can be used only inside awk. The shell variable NF has only the same name as the awk variable.

– Walter A
Jan 1 at 13:41













temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

– Walter A
Jan 1 at 13:43







temp=$(sed 's/.* //' aa) removing everything until (including) the last space.

– Walter A
Jan 1 at 13:43














2 Answers
2






active

oldest

votes


















0














Something like that?



temp=$(awk '{print $NF}' aa)





share|improve this answer
























  • What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

    – user1107731
    Jan 1 at 14:53



















0














shell version (ksh, bash)



temp='Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD'
temp="${temp##* }"


# or in ssh script using output
temp="$( cat aa )"
echo "${temp##* }"





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%2f53995344%2fhow-to-use-awk-build-in-variable-in-shell-script%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









    0














    Something like that?



    temp=$(awk '{print $NF}' aa)





    share|improve this answer
























    • What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

      – user1107731
      Jan 1 at 14:53
















    0














    Something like that?



    temp=$(awk '{print $NF}' aa)





    share|improve this answer
























    • What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

      – user1107731
      Jan 1 at 14:53














    0












    0








    0







    Something like that?



    temp=$(awk '{print $NF}' aa)





    share|improve this answer













    Something like that?



    temp=$(awk '{print $NF}' aa)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 1 at 13:28









    CyrusCyrus

    46.2k43878




    46.2k43878













    • What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

      – user1107731
      Jan 1 at 14:53



















    • What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

      – user1107731
      Jan 1 at 14:53

















    What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

    – user1107731
    Jan 1 at 14:53





    What if the string is not present in the file. The output in the string is got when I execute a command like "some chn ". This will give list of all links present in the server. Some 100 strings like I listed in the example. I want to get the links which has zero traffic. The link is the last element present in each row. What I want is something like this "some chn" | grep ""Load= 0.000" |awk -v temp=$(awk '{print $NF}' )'{print $temp}' . But this does not work

    – user1107731
    Jan 1 at 14:53













    0














    shell version (ksh, bash)



    temp='Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD'
    temp="${temp##* }"


    # or in ssh script using output
    temp="$( cat aa )"
    echo "${temp##* }"





    share|improve this answer




























      0














      shell version (ksh, bash)



      temp='Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD'
      temp="${temp##* }"


      # or in ssh script using output
      temp="$( cat aa )"
      echo "${temp##* }"





      share|improve this answer


























        0












        0








        0







        shell version (ksh, bash)



        temp='Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD'
        temp="${temp##* }"


        # or in ssh script using output
        temp="$( cat aa )"
        echo "${temp##* }"





        share|improve this answer













        shell version (ksh, bash)



        temp='Chapl 222222/0: Pran=28e2 Load=0.000 kkk Statevfe=Nowf Alarmkj fIMmg=1 LkouIM="087" Po89rt="eth566" NE40Ef-02-T1M1N2-HUW_S1_RD'
        temp="${temp##* }"


        # or in ssh script using output
        temp="$( cat aa )"
        echo "${temp##* }"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 14:51









        NeronLeVeluNeronLeVelu

        8,66011737




        8,66011737






























            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%2f53995344%2fhow-to-use-awk-build-in-variable-in-shell-script%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas