how to assign values of the column for given conditional statements











up vote
0
down vote

favorite












I need a script to assign values of the column with a given conditional argument. To be specific, here is my data and my condition



input data.file:



0.4
3.6
-2.4
0.1
-2.0
1.6
2.2
3.6


condition: assign R if data.file greater than 2(>2), assign S if data.file less than -2(<-2), and assign M if data.file greater than or equal to-2 and less than or equal to 2 [-2,2].



Desired output file out.file



0.4  M
3.6 R
-2.4 S
0.1 M
-2.0 M
1.6 M
2.2 R
3.6 R









share|improve this question




















  • 1




    so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
    – umläute
    Sep 18 '13 at 11:08















up vote
0
down vote

favorite












I need a script to assign values of the column with a given conditional argument. To be specific, here is my data and my condition



input data.file:



0.4
3.6
-2.4
0.1
-2.0
1.6
2.2
3.6


condition: assign R if data.file greater than 2(>2), assign S if data.file less than -2(<-2), and assign M if data.file greater than or equal to-2 and less than or equal to 2 [-2,2].



Desired output file out.file



0.4  M
3.6 R
-2.4 S
0.1 M
-2.0 M
1.6 M
2.2 R
3.6 R









share|improve this question




















  • 1




    so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
    – umläute
    Sep 18 '13 at 11:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need a script to assign values of the column with a given conditional argument. To be specific, here is my data and my condition



input data.file:



0.4
3.6
-2.4
0.1
-2.0
1.6
2.2
3.6


condition: assign R if data.file greater than 2(>2), assign S if data.file less than -2(<-2), and assign M if data.file greater than or equal to-2 and less than or equal to 2 [-2,2].



Desired output file out.file



0.4  M
3.6 R
-2.4 S
0.1 M
-2.0 M
1.6 M
2.2 R
3.6 R









share|improve this question















I need a script to assign values of the column with a given conditional argument. To be specific, here is my data and my condition



input data.file:



0.4
3.6
-2.4
0.1
-2.0
1.6
2.2
3.6


condition: assign R if data.file greater than 2(>2), assign S if data.file less than -2(<-2), and assign M if data.file greater than or equal to-2 and less than or equal to 2 [-2,2].



Desired output file out.file



0.4  M
3.6 R
-2.4 S
0.1 M
-2.0 M
1.6 M
2.2 R
3.6 R






text-processing awk columns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Sep 18 '13 at 10:59









AiB

3472620




3472620








  • 1




    so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
    – umläute
    Sep 18 '13 at 11:08














  • 1




    so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
    – umläute
    Sep 18 '13 at 11:08








1




1




so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
– umläute
Sep 18 '13 at 11:08




so what's the problem? have you tried anything? Stack Overflow might be a good place to ask programming specific questions.
– umläute
Sep 18 '13 at 11:08










1 Answer
1






active

oldest

votes

















up vote
3
down vote













read your file line by line, check whether the value falls into a given category and output accordingly.



 awk '{if($1>2)print $1,"R"; else if ($1<-2)print $1,"S"; else print $1,"M"}'





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f91224%2fhow-to-assign-values-of-the-column-for-given-conditional-statements%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








    up vote
    3
    down vote













    read your file line by line, check whether the value falls into a given category and output accordingly.



     awk '{if($1>2)print $1,"R"; else if ($1<-2)print $1,"S"; else print $1,"M"}'





    share|improve this answer

























      up vote
      3
      down vote













      read your file line by line, check whether the value falls into a given category and output accordingly.



       awk '{if($1>2)print $1,"R"; else if ($1<-2)print $1,"S"; else print $1,"M"}'





      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        read your file line by line, check whether the value falls into a given category and output accordingly.



         awk '{if($1>2)print $1,"R"; else if ($1<-2)print $1,"S"; else print $1,"M"}'





        share|improve this answer












        read your file line by line, check whether the value falls into a given category and output accordingly.



         awk '{if($1>2)print $1,"R"; else if ($1<-2)print $1,"S"; else print $1,"M"}'






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 18 '13 at 11:13









        umläute

        4,5381433




        4,5381433






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f91224%2fhow-to-assign-values-of-the-column-for-given-conditional-statements%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

            Entries order in /etc/network/interfaces

            新発田市

            Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)