How to compare the content inside a txt file?












-1















I have a txt file which named as output.txt:



output.txt

2128,4.4
2128,5.5


I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.



output:
5.5


If the output.txt have different number for first line first number and second line first number, for example:



output.txt
2622,56
1011,65


Then expected output will be return a stringThe value is different










share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What have you tried?

    – Nasir Riley
    49 mins ago











  • sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

    – Shi Jie Tio
    47 mins ago











  • sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

    – Shi Jie Tio
    44 mins ago
















-1















I have a txt file which named as output.txt:



output.txt

2128,4.4
2128,5.5


I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.



output:
5.5


If the output.txt have different number for first line first number and second line first number, for example:



output.txt
2622,56
1011,65


Then expected output will be return a stringThe value is different










share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • What have you tried?

    – Nasir Riley
    49 mins ago











  • sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

    – Shi Jie Tio
    47 mins ago











  • sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

    – Shi Jie Tio
    44 mins ago














-1












-1








-1








I have a txt file which named as output.txt:



output.txt

2128,4.4
2128,5.5


I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.



output:
5.5


If the output.txt have different number for first line first number and second line first number, for example:



output.txt
2622,56
1011,65


Then expected output will be return a stringThe value is different










share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have a txt file which named as output.txt:



output.txt

2128,4.4
2128,5.5


I wish to have a compare script that first line first number before comma is equal to second line first number, then it will return second line second number after comma.



output:
5.5


If the output.txt have different number for first line first number and second line first number, for example:



output.txt
2622,56
1011,65


Then expected output will be return a stringThe value is different







shell-script text-processing






share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









Shi Jie TioShi Jie Tio

1012




1012




New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • What have you tried?

    – Nasir Riley
    49 mins ago











  • sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

    – Shi Jie Tio
    47 mins ago











  • sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

    – Shi Jie Tio
    44 mins ago



















  • What have you tried?

    – Nasir Riley
    49 mins ago











  • sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

    – Shi Jie Tio
    47 mins ago











  • sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

    – Shi Jie Tio
    44 mins ago

















What have you tried?

– Nasir Riley
49 mins ago





What have you tried?

– Nasir Riley
49 mins ago













sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

– Shi Jie Tio
47 mins ago





sed -e 's/^(.{4}).*/1/' out.txt but this will print out all line with first 4 integer

– Shi Jie Tio
47 mins ago













sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

– Shi Jie Tio
44 mins ago





sed -e 's/,.*//' output.txt and this will print out all the line with the value before comma, I wish to just retrieve first line or second line so that i can make comparison, anyone can share ideas?

– Shi Jie Tio
44 mins ago










1 Answer
1






active

oldest

votes


















0














Confirmed in bash:



#!/bin/bash

file=/path/to/output.txt

if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then

awk -F , 'NR==2 {print $2}' $text

else

echo "The value is different"

fi


-The variable file is assigned to the path of wherever output.txt is located.



-In the if statement, awk uses , as the delimiter and prints and compares the second values of the second column on each line.



-If the values are equal, then it prints the second column of the second line which is 5.5



-If the values are not equal, then it prints "The value is different".



I tested this with two files with the values that you specified. You can change the value of the file variable in the script to work with others.






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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });






    Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494344%2fhow-to-compare-the-content-inside-a-txt-file%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














    Confirmed in bash:



    #!/bin/bash

    file=/path/to/output.txt

    if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then

    awk -F , 'NR==2 {print $2}' $text

    else

    echo "The value is different"

    fi


    -The variable file is assigned to the path of wherever output.txt is located.



    -In the if statement, awk uses , as the delimiter and prints and compares the second values of the second column on each line.



    -If the values are equal, then it prints the second column of the second line which is 5.5



    -If the values are not equal, then it prints "The value is different".



    I tested this with two files with the values that you specified. You can change the value of the file variable in the script to work with others.






    share|improve this answer




























      0














      Confirmed in bash:



      #!/bin/bash

      file=/path/to/output.txt

      if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then

      awk -F , 'NR==2 {print $2}' $text

      else

      echo "The value is different"

      fi


      -The variable file is assigned to the path of wherever output.txt is located.



      -In the if statement, awk uses , as the delimiter and prints and compares the second values of the second column on each line.



      -If the values are equal, then it prints the second column of the second line which is 5.5



      -If the values are not equal, then it prints "The value is different".



      I tested this with two files with the values that you specified. You can change the value of the file variable in the script to work with others.






      share|improve this answer


























        0












        0








        0







        Confirmed in bash:



        #!/bin/bash

        file=/path/to/output.txt

        if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then

        awk -F , 'NR==2 {print $2}' $text

        else

        echo "The value is different"

        fi


        -The variable file is assigned to the path of wherever output.txt is located.



        -In the if statement, awk uses , as the delimiter and prints and compares the second values of the second column on each line.



        -If the values are equal, then it prints the second column of the second line which is 5.5



        -If the values are not equal, then it prints "The value is different".



        I tested this with two files with the values that you specified. You can change the value of the file variable in the script to work with others.






        share|improve this answer













        Confirmed in bash:



        #!/bin/bash

        file=/path/to/output.txt

        if [[ $(awk -F , 'NR==1 {print $1}' $file) -eq $(awk -F , 'NR==2 {print $1}' $file) ]]; then

        awk -F , 'NR==2 {print $2}' $text

        else

        echo "The value is different"

        fi


        -The variable file is assigned to the path of wherever output.txt is located.



        -In the if statement, awk uses , as the delimiter and prints and compares the second values of the second column on each line.



        -If the values are equal, then it prints the second column of the second line which is 5.5



        -If the values are not equal, then it prints "The value is different".



        I tested this with two files with the values that you specified. You can change the value of the file variable in the script to work with others.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 15 mins ago









        Nasir RileyNasir Riley

        2,406239




        2,406239






















            Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.













            Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.












            Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f494344%2fhow-to-compare-the-content-inside-a-txt-file%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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt