Using AWK to get second column

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











up vote
9
down vote

favorite












I can not seem to get the awk command to get the second column of data.



Bash Code:



filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)

printf "$filter_data n"


The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.



The file contents are:



Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s


I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.










share|improve this question



















  • 2




    Why save it to variable if you're printing it right away ? Just use awk's printf
    – Sergiy Kolodyazhnyy
    Sep 12 at 21:31














up vote
9
down vote

favorite












I can not seem to get the awk command to get the second column of data.



Bash Code:



filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)

printf "$filter_data n"


The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.



The file contents are:



Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s


I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.










share|improve this question



















  • 2




    Why save it to variable if you're printing it right away ? Just use awk's printf
    – Sergiy Kolodyazhnyy
    Sep 12 at 21:31












up vote
9
down vote

favorite









up vote
9
down vote

favorite











I can not seem to get the awk command to get the second column of data.



Bash Code:



filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)

printf "$filter_data n"


The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.



The file contents are:



Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s


I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.










share|improve this question















I can not seem to get the awk command to get the second column of data.



Bash Code:



filter_data=$(awk "if(/$filter:/) print $2" < scanresults_temp.txt)

printf "$filter_data n"


The $filter variable is either the value of Download or Upload that gets passed into the shell script. So awk uses the term Download or Upload to search for the proper row.



The file contents are:



Testing download speed................................................................................
Download: 51.13 Mbit/s
Testing upload speed................................................................................................
Upload: 57.38 Mbit/s


I am trying to get just the numbers and not anything else, ex, 51.13 and 57.38.







bash grep sed awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 12 at 20:04









dessert

20k55795




20k55795










asked Sep 12 at 19:57









Vlad

483




483







  • 2




    Why save it to variable if you're printing it right away ? Just use awk's printf
    – Sergiy Kolodyazhnyy
    Sep 12 at 21:31












  • 2




    Why save it to variable if you're printing it right away ? Just use awk's printf
    – Sergiy Kolodyazhnyy
    Sep 12 at 21:31







2




2




Why save it to variable if you're printing it right away ? Just use awk's printf
– Sergiy Kolodyazhnyy
Sep 12 at 21:31




Why save it to variable if you're printing it right away ? Just use awk's printf
– Sergiy Kolodyazhnyy
Sep 12 at 21:31










2 Answers
2






active

oldest

votes

















up vote
13
down vote



accepted










You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
Here's how you should pass a shell variable into awk:



filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )





share|improve this answer






















  • Perfect, thank you.
    – Vlad
    Sep 12 at 20:13

















up vote
7
down vote













Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):



re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt


Note that variable quoting isn't necessary when performing assignments in shell






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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f1074751%2fusing-awk-to-get-second-column%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    13
    down vote



    accepted










    You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
    Here's how you should pass a shell variable into awk:



    filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )





    share|improve this answer






















    • Perfect, thank you.
      – Vlad
      Sep 12 at 20:13














    up vote
    13
    down vote



    accepted










    You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
    Here's how you should pass a shell variable into awk:



    filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )





    share|improve this answer






















    • Perfect, thank you.
      – Vlad
      Sep 12 at 20:13












    up vote
    13
    down vote



    accepted







    up vote
    13
    down vote



    accepted






    You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
    Here's how you should pass a shell variable into awk:



    filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )





    share|improve this answer














    You need to use single quotes for the awk program body so awk's variables are not expanded by the shell.
    Here's how you should pass a shell variable into awk:



    filter_data=$( awk -v re="$filter:" '$0 ~ re print $2' scanresults_temp.txt )






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 12 at 21:35









    Sergiy Kolodyazhnyy

    65.8k9134287




    65.8k9134287










    answered Sep 12 at 20:05









    glenn jackman

    12.1k2442




    12.1k2442











    • Perfect, thank you.
      – Vlad
      Sep 12 at 20:13
















    • Perfect, thank you.
      – Vlad
      Sep 12 at 20:13















    Perfect, thank you.
    – Vlad
    Sep 12 at 20:13




    Perfect, thank you.
    – Vlad
    Sep 12 at 20:13












    up vote
    7
    down vote













    Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):



    re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt


    Note that variable quoting isn't necessary when performing assignments in shell






    share|improve this answer
























      up vote
      7
      down vote













      Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):



      re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt


      Note that variable quoting isn't necessary when performing assignments in shell






      share|improve this answer






















        up vote
        7
        down vote










        up vote
        7
        down vote









        Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):



        re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt


        Note that variable quoting isn't necessary when performing assignments in shell






        share|improve this answer












        Alternative way is to let shell put variable into awks environment via proceeding assignment ( temporary ):



        re=$filter awk '$0 ~ ENVIRON["re"] print $2' input.txt


        Note that variable quoting isn't necessary when performing assignments in shell







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 12 at 21:35









        Sergiy Kolodyazhnyy

        65.8k9134287




        65.8k9134287



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1074751%2fusing-awk-to-get-second-column%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)