Read value from specific row and column










1














I have csv file in this format:



1 value1
2 value2
3 value3


I can also define it like this (if it is more convenient)



column1,column2
1,value1
2,value2
3,value3


I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1 then take and use (or print) value1.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks










share|improve this question



















  • 2




    use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
    – Red Cricket
    Nov 12 '18 at 23:15











  • Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
    – Dejan
    Nov 12 '18 at 23:17










  • the ^ mean match the start of a line. So ^1 means match line that starts with 1
    – Red Cricket
    Nov 12 '18 at 23:18










  • Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
    – Dejan
    Nov 12 '18 at 23:20










  • try it and see.
    – Red Cricket
    Nov 12 '18 at 23:21















1














I have csv file in this format:



1 value1
2 value2
3 value3


I can also define it like this (if it is more convenient)



column1,column2
1,value1
2,value2
3,value3


I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1 then take and use (or print) value1.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks










share|improve this question



















  • 2




    use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
    – Red Cricket
    Nov 12 '18 at 23:15











  • Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
    – Dejan
    Nov 12 '18 at 23:17










  • the ^ mean match the start of a line. So ^1 means match line that starts with 1
    – Red Cricket
    Nov 12 '18 at 23:18










  • Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
    – Dejan
    Nov 12 '18 at 23:20










  • try it and see.
    – Red Cricket
    Nov 12 '18 at 23:21













1












1








1







I have csv file in this format:



1 value1
2 value2
3 value3


I can also define it like this (if it is more convenient)



column1,column2
1,value1
2,value2
3,value3


I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1 then take and use (or print) value1.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks










share|improve this question















I have csv file in this format:



1 value1
2 value2
3 value3


I can also define it like this (if it is more convenient)



column1,column2
1,value1
2,value2
3,value3


I should write a shell script in order to match value from the first column and take value from that row from the second column value.
So for example, I have variable var
If var is equal to 1 then take and use (or print) value1.
If value is 2 take value2 and so on.
Number of rows is limited to 5.
Is this possible?
Thanks







linux bash shell centos sh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:47









codeforester

17.4k83864




17.4k83864










asked Nov 12 '18 at 23:11









DejanDejan

57082148




57082148







  • 2




    use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
    – Red Cricket
    Nov 12 '18 at 23:15











  • Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
    – Dejan
    Nov 12 '18 at 23:17










  • the ^ mean match the start of a line. So ^1 means match line that starts with 1
    – Red Cricket
    Nov 12 '18 at 23:18










  • Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
    – Dejan
    Nov 12 '18 at 23:20










  • try it and see.
    – Red Cricket
    Nov 12 '18 at 23:21












  • 2




    use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
    – Red Cricket
    Nov 12 '18 at 23:15











  • Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
    – Dejan
    Nov 12 '18 at 23:17










  • the ^ mean match the start of a line. So ^1 means match line that starts with 1
    – Red Cricket
    Nov 12 '18 at 23:18










  • Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
    – Dejan
    Nov 12 '18 at 23:20










  • try it and see.
    – Red Cricket
    Nov 12 '18 at 23:21







2




2




use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15





use grep and cut like so: var=1 ; grep "^$var" yourfile.txt | cut -d',' -f2
– Red Cricket
Nov 12 '18 at 23:15













Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17




Hi Red. For what purposes is this used "^1"? how to match with my variable which will be read within the script? Thanks
– Dejan
Nov 12 '18 at 23:17












the ^ mean match the start of a line. So ^1 means match line that starts with 1
– Red Cricket
Nov 12 '18 at 23:18




the ^ mean match the start of a line. So ^1 means match line that starts with 1
– Red Cricket
Nov 12 '18 at 23:18












Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20




Hi Red. but what if my value2 for example is something like this "1.1.1". Does it mean if I grep it with value "1" it will grep me the frst row (because of the first row first column value) but also and second row (because it is similar to second row second column value )? thanks
– Dejan
Nov 12 '18 at 23:20












try it and see.
– Red Cricket
Nov 12 '18 at 23:21




try it and see.
– Red Cricket
Nov 12 '18 at 23:21












1 Answer
1






active

oldest

votes


















1














Awk is your friend here:



$ cat input.txt
1 value1
2 value2
3 value3
$ awk -v key=1 '$1 == key print $2 ' input.txt
value1
$ awk -v key=2 '$1 == key print $2 ' input.txt
value2


etc. Just replace the key=1 with key=$var.






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%2f53271435%2fread-value-from-specific-row-and-column%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














    Awk is your friend here:



    $ cat input.txt
    1 value1
    2 value2
    3 value3
    $ awk -v key=1 '$1 == key print $2 ' input.txt
    value1
    $ awk -v key=2 '$1 == key print $2 ' input.txt
    value2


    etc. Just replace the key=1 with key=$var.






    share|improve this answer

























      1














      Awk is your friend here:



      $ cat input.txt
      1 value1
      2 value2
      3 value3
      $ awk -v key=1 '$1 == key print $2 ' input.txt
      value1
      $ awk -v key=2 '$1 == key print $2 ' input.txt
      value2


      etc. Just replace the key=1 with key=$var.






      share|improve this answer























        1












        1








        1






        Awk is your friend here:



        $ cat input.txt
        1 value1
        2 value2
        3 value3
        $ awk -v key=1 '$1 == key print $2 ' input.txt
        value1
        $ awk -v key=2 '$1 == key print $2 ' input.txt
        value2


        etc. Just replace the key=1 with key=$var.






        share|improve this answer












        Awk is your friend here:



        $ cat input.txt
        1 value1
        2 value2
        3 value3
        $ awk -v key=1 '$1 == key print $2 ' input.txt
        value1
        $ awk -v key=2 '$1 == key print $2 ' input.txt
        value2


        etc. Just replace the key=1 with key=$var.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 23:36









        ShawnShawn

        3,5731613




        3,5731613



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53271435%2fread-value-from-specific-row-and-column%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto