Sed-Grep-Awk operationsUsing awk to identify the number identical columnsIf column matches another file,...

How can changes in personality/values of a person who turned into a vampire be explained?

PostGIS function to move a polygon to centre over new point coordinates

Why is perturbation theory used in quantum mechanics?

Why does this quiz question say that protons and electrons do not combine to form neutrons?

How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?

In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?

How to Build a List from Separate Lists

Have any astronauts or cosmonauts died in space?

Coworker asking me to not bring cakes due to self control issue. What should I do?

Can I legally make a website about boycotting a certain company?

Is Screenshot Time-tracking Common?

Is there a configuration of the 8-puzzle where locking a tile makes it harder?

Why is Shelob considered evil?

Sed-Grep-Awk operations

How can a Sorcerer/Warlock use 4 Eldritch blasts in one round?

What could cause an entire planet of humans to become aphasic?

Integral problem. Unsure of the approach.

Does the double-bladed scimitar's special attack let you use your ability modifier for the damage of the attack?

Can a planet be tidally unlocked?

Buying a "Used" Router

Trying to make a 3dplot

Multiple null checks in Java 8

Is layered encryption more secure than long passwords?

Why is quixotic not Quixotic (a proper adjective)?



Sed-Grep-Awk operations


Using awk to identify the number identical columnsIf column matches another file, print every line with match (awk/grep)Multiline Regexp (grep, sed, awk, perl)Grep output concernspattern file as an input to RS,FS in awk/sed/grep to recognize and add columnsawk from different linesmatch two columns from one file to three columns from another file, print out lines from the file with two columnsextract columns from TRUE/FALSE matrix based on proportion of TRUE values within the columnPrinting columns using AWK?Column manipulation using AWK













2















A file contains 5 columns with numbers



Example:



12 34 67 88 10
4 90 12 10 7
33 12 5 76 34


I would like to print the same number and see how many times it goes out.
Example:



3 : 12
2 : 34









share|improve this question

























  • presumably only for numbers that occur more than once?

    – Jeff Schaller
    49 mins ago











  • Numbers, or digits? How do you arrive at that output?

    – Kusalananda
    37 mins ago
















2















A file contains 5 columns with numbers



Example:



12 34 67 88 10
4 90 12 10 7
33 12 5 76 34


I would like to print the same number and see how many times it goes out.
Example:



3 : 12
2 : 34









share|improve this question

























  • presumably only for numbers that occur more than once?

    – Jeff Schaller
    49 mins ago











  • Numbers, or digits? How do you arrive at that output?

    – Kusalananda
    37 mins ago














2












2








2








A file contains 5 columns with numbers



Example:



12 34 67 88 10
4 90 12 10 7
33 12 5 76 34


I would like to print the same number and see how many times it goes out.
Example:



3 : 12
2 : 34









share|improve this question
















A file contains 5 columns with numbers



Example:



12 34 67 88 10
4 90 12 10 7
33 12 5 76 34


I would like to print the same number and see how many times it goes out.
Example:



3 : 12
2 : 34






awk sed grep numeric-data






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 50 mins ago









Jeff Schaller

41.8k1156133




41.8k1156133










asked 55 mins ago









InsideMiamiTattooInsideMiamiTattoo

162




162













  • presumably only for numbers that occur more than once?

    – Jeff Schaller
    49 mins ago











  • Numbers, or digits? How do you arrive at that output?

    – Kusalananda
    37 mins ago



















  • presumably only for numbers that occur more than once?

    – Jeff Schaller
    49 mins ago











  • Numbers, or digits? How do you arrive at that output?

    – Kusalananda
    37 mins ago

















presumably only for numbers that occur more than once?

– Jeff Schaller
49 mins ago





presumably only for numbers that occur more than once?

– Jeff Schaller
49 mins ago













Numbers, or digits? How do you arrive at that output?

– Kusalananda
37 mins ago





Numbers, or digits? How do you arrive at that output?

– Kusalananda
37 mins ago










2 Answers
2






active

oldest

votes


















4














This awk script prints output as in your example:



awk '{ 
for ( i=1; i<=NF; i++ ) # loop over all fields/columns
dict[$i]++; # count occurrence in an array using the field value as index/key
}
END { # after processing all data
for (key in dict) # iterate over all array keys
if(dict[key]>1) # if the key occurred more than once
print dict[key] " : " key # print counter and key
}' inputfile


With the example input, the output is



2 : 10
3 : 12
2 : 34


If you remove the condition if(a[i]>1) it will also list numbers that occurred only once.






share|improve this answer

































    3














    You could use a pipeline



    tr ' ' 'n' < datafile | sort | uniq -c


    Forging on how refined you want your answer you could filter for numeric values or values where the count is more than one.






    share|improve this answer
























    • You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

      – Bodo
      12 mins ago











    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502339%2fsed-grep-awk-operations%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









    4














    This awk script prints output as in your example:



    awk '{ 
    for ( i=1; i<=NF; i++ ) # loop over all fields/columns
    dict[$i]++; # count occurrence in an array using the field value as index/key
    }
    END { # after processing all data
    for (key in dict) # iterate over all array keys
    if(dict[key]>1) # if the key occurred more than once
    print dict[key] " : " key # print counter and key
    }' inputfile


    With the example input, the output is



    2 : 10
    3 : 12
    2 : 34


    If you remove the condition if(a[i]>1) it will also list numbers that occurred only once.






    share|improve this answer






























      4














      This awk script prints output as in your example:



      awk '{ 
      for ( i=1; i<=NF; i++ ) # loop over all fields/columns
      dict[$i]++; # count occurrence in an array using the field value as index/key
      }
      END { # after processing all data
      for (key in dict) # iterate over all array keys
      if(dict[key]>1) # if the key occurred more than once
      print dict[key] " : " key # print counter and key
      }' inputfile


      With the example input, the output is



      2 : 10
      3 : 12
      2 : 34


      If you remove the condition if(a[i]>1) it will also list numbers that occurred only once.






      share|improve this answer




























        4












        4








        4







        This awk script prints output as in your example:



        awk '{ 
        for ( i=1; i<=NF; i++ ) # loop over all fields/columns
        dict[$i]++; # count occurrence in an array using the field value as index/key
        }
        END { # after processing all data
        for (key in dict) # iterate over all array keys
        if(dict[key]>1) # if the key occurred more than once
        print dict[key] " : " key # print counter and key
        }' inputfile


        With the example input, the output is



        2 : 10
        3 : 12
        2 : 34


        If you remove the condition if(a[i]>1) it will also list numbers that occurred only once.






        share|improve this answer















        This awk script prints output as in your example:



        awk '{ 
        for ( i=1; i<=NF; i++ ) # loop over all fields/columns
        dict[$i]++; # count occurrence in an array using the field value as index/key
        }
        END { # after processing all data
        for (key in dict) # iterate over all array keys
        if(dict[key]>1) # if the key occurred more than once
        print dict[key] " : " key # print counter and key
        }' inputfile


        With the example input, the output is



        2 : 10
        3 : 12
        2 : 34


        If you remove the condition if(a[i]>1) it will also list numbers that occurred only once.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 34 mins ago

























        answered 39 mins ago









        BodoBodo

        1,645212




        1,645212

























            3














            You could use a pipeline



            tr ' ' 'n' < datafile | sort | uniq -c


            Forging on how refined you want your answer you could filter for numeric values or values where the count is more than one.






            share|improve this answer
























            • You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

              – Bodo
              12 mins ago
















            3














            You could use a pipeline



            tr ' ' 'n' < datafile | sort | uniq -c


            Forging on how refined you want your answer you could filter for numeric values or values where the count is more than one.






            share|improve this answer
























            • You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

              – Bodo
              12 mins ago














            3












            3








            3







            You could use a pipeline



            tr ' ' 'n' < datafile | sort | uniq -c


            Forging on how refined you want your answer you could filter for numeric values or values where the count is more than one.






            share|improve this answer













            You could use a pipeline



            tr ' ' 'n' < datafile | sort | uniq -c


            Forging on how refined you want your answer you could filter for numeric values or values where the count is more than one.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 33 mins ago









            roaimaroaima

            44.8k756122




            44.8k756122













            • You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

              – Bodo
              12 mins ago



















            • You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

              – Bodo
              12 mins ago

















            You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

            – Bodo
            12 mins ago





            You could append | awk '($1 > 1) && ($2 > 0) { print $1 " : " $2 }' to get output similar to the example in the question.

            – Bodo
            12 mins ago


















            draft saved

            draft discarded




















































            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%2f502339%2fsed-grep-awk-operations%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

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

            Meter-Bus Содержание Параметры шины | Стандартизация |...