Retrieve first occurrence of record, where matching pattern is taken from input

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











up vote
0
down vote

favorite












I have a list like this:



2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.


I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:



2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638


The list is already sorted by column 2 ascending, then column 1 descending.



I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.



This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.



I fail connecting the dots. How would you do it?







share|improve this question


















  • 2




    awk '!seen[$2]++' list_file
    – don_crissti
    Dec 26 '17 at 0:06














up vote
0
down vote

favorite












I have a list like this:



2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.


I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:



2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638


The list is already sorted by column 2 ascending, then column 1 descending.



I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.



This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.



I fail connecting the dots. How would you do it?







share|improve this question


















  • 2




    awk '!seen[$2]++' list_file
    – don_crissti
    Dec 26 '17 at 0:06












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a list like this:



2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.


I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:



2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638


The list is already sorted by column 2 ascending, then column 1 descending.



I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.



This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.



I fail connecting the dots. How would you do it?







share|improve this question














I have a list like this:



2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.


I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:



2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638


The list is already sorted by column 2 ascending, then column 1 descending.



I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.



This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.



I fail connecting the dots. How would you do it?









share|improve this question













share|improve this question




share|improve this question








edited Dec 26 '17 at 2:19









Jeff Schaller

31.8k848109




31.8k848109










asked Dec 26 '17 at 0:00









HenrikJson

257




257







  • 2




    awk '!seen[$2]++' list_file
    – don_crissti
    Dec 26 '17 at 0:06












  • 2




    awk '!seen[$2]++' list_file
    – don_crissti
    Dec 26 '17 at 0:06







2




2




awk '!seen[$2]++' list_file
– don_crissti
Dec 26 '17 at 0:06




awk '!seen[$2]++' list_file
– don_crissti
Dec 26 '17 at 0:06










2 Answers
2






active

oldest

votes

















up vote
2
down vote













Two ways to do it:



sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile






share|improve this answer



























    up vote
    0
    down vote













    I have done this by awk and sed combination.




    for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done 



    output




    2017-12-11 AAOI 40.33
    2017-12-15 AEIS 70.98
    2017-10-23 AIEQ 25.1601
    2017-11-15 AMBA 52.6501
    2017-12-05 ATHM 57.2
    2017-11-09 AUDC 7.02
    2017-12-22 BEW 0.58
    2017-10-17 BIOP 8.19
    2017-12-08 BLDP 4.86
    2017-12-21 BLOC 2.3
    2017-11-15 BOX 21.63
    2017-12-22 BTL 10.5638







    share|improve this answer




















    • Have a UUoC award
      – Fox
      Dec 26 '17 at 10:50










    • @Fox didnt get ?
      – Praveen Kumar BS
      Dec 26 '17 at 16:26










    • There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
      – Fox
      Dec 26 '17 at 16:30










    • yes agreed @Fox
      – Praveen Kumar BS
      Dec 26 '17 at 16:32










    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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f413038%2fretrieve-first-occurrence-of-record-where-matching-pattern-is-taken-from-input%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
    2
    down vote













    Two ways to do it:



    sort sort -u -k2,2 infile
    awk awk -F" " '!_[$2]++' infile






    share|improve this answer
























      up vote
      2
      down vote













      Two ways to do it:



      sort sort -u -k2,2 infile
      awk awk -F" " '!_[$2]++' infile






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        Two ways to do it:



        sort sort -u -k2,2 infile
        awk awk -F" " '!_[$2]++' infile






        share|improve this answer












        Two ways to do it:



        sort sort -u -k2,2 infile
        awk awk -F" " '!_[$2]++' infile







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 26 '17 at 0:20









        Isaac

        6,7911834




        6,7911834






















            up vote
            0
            down vote













            I have done this by awk and sed combination.




            for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done 



            output




            2017-12-11 AAOI 40.33
            2017-12-15 AEIS 70.98
            2017-10-23 AIEQ 25.1601
            2017-11-15 AMBA 52.6501
            2017-12-05 ATHM 57.2
            2017-11-09 AUDC 7.02
            2017-12-22 BEW 0.58
            2017-10-17 BIOP 8.19
            2017-12-08 BLDP 4.86
            2017-12-21 BLOC 2.3
            2017-11-15 BOX 21.63
            2017-12-22 BTL 10.5638







            share|improve this answer




















            • Have a UUoC award
              – Fox
              Dec 26 '17 at 10:50










            • @Fox didnt get ?
              – Praveen Kumar BS
              Dec 26 '17 at 16:26










            • There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
              – Fox
              Dec 26 '17 at 16:30










            • yes agreed @Fox
              – Praveen Kumar BS
              Dec 26 '17 at 16:32














            up vote
            0
            down vote













            I have done this by awk and sed combination.




            for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done 



            output




            2017-12-11 AAOI 40.33
            2017-12-15 AEIS 70.98
            2017-10-23 AIEQ 25.1601
            2017-11-15 AMBA 52.6501
            2017-12-05 ATHM 57.2
            2017-11-09 AUDC 7.02
            2017-12-22 BEW 0.58
            2017-10-17 BIOP 8.19
            2017-12-08 BLDP 4.86
            2017-12-21 BLOC 2.3
            2017-11-15 BOX 21.63
            2017-12-22 BTL 10.5638







            share|improve this answer




















            • Have a UUoC award
              – Fox
              Dec 26 '17 at 10:50










            • @Fox didnt get ?
              – Praveen Kumar BS
              Dec 26 '17 at 16:26










            • There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
              – Fox
              Dec 26 '17 at 16:30










            • yes agreed @Fox
              – Praveen Kumar BS
              Dec 26 '17 at 16:32












            up vote
            0
            down vote










            up vote
            0
            down vote









            I have done this by awk and sed combination.




            for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done 



            output




            2017-12-11 AAOI 40.33
            2017-12-15 AEIS 70.98
            2017-10-23 AIEQ 25.1601
            2017-11-15 AMBA 52.6501
            2017-12-05 ATHM 57.2
            2017-11-09 AUDC 7.02
            2017-12-22 BEW 0.58
            2017-10-17 BIOP 8.19
            2017-12-08 BLDP 4.86
            2017-12-21 BLOC 2.3
            2017-11-15 BOX 21.63
            2017-12-22 BTL 10.5638







            share|improve this answer












            I have done this by awk and sed combination.




            for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done 



            output




            2017-12-11 AAOI 40.33
            2017-12-15 AEIS 70.98
            2017-10-23 AIEQ 25.1601
            2017-11-15 AMBA 52.6501
            2017-12-05 ATHM 57.2
            2017-11-09 AUDC 7.02
            2017-12-22 BEW 0.58
            2017-10-17 BIOP 8.19
            2017-12-08 BLDP 4.86
            2017-12-21 BLOC 2.3
            2017-11-15 BOX 21.63
            2017-12-22 BTL 10.5638








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 26 '17 at 5:21









            Praveen Kumar BS

            1,010128




            1,010128











            • Have a UUoC award
              – Fox
              Dec 26 '17 at 10:50










            • @Fox didnt get ?
              – Praveen Kumar BS
              Dec 26 '17 at 16:26










            • There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
              – Fox
              Dec 26 '17 at 16:30










            • yes agreed @Fox
              – Praveen Kumar BS
              Dec 26 '17 at 16:32
















            • Have a UUoC award
              – Fox
              Dec 26 '17 at 10:50










            • @Fox didnt get ?
              – Praveen Kumar BS
              Dec 26 '17 at 16:26










            • There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
              – Fox
              Dec 26 '17 at 16:30










            • yes agreed @Fox
              – Praveen Kumar BS
              Dec 26 '17 at 16:32















            Have a UUoC award
            – Fox
            Dec 26 '17 at 10:50




            Have a UUoC award
            – Fox
            Dec 26 '17 at 10:50












            @Fox didnt get ?
            – Praveen Kumar BS
            Dec 26 '17 at 16:26




            @Fox didnt get ?
            – Praveen Kumar BS
            Dec 26 '17 at 16:26












            There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
            – Fox
            Dec 26 '17 at 16:30




            There's no need for cat here, both because shell redirection exists and because awk takes a filename argument
            – Fox
            Dec 26 '17 at 16:30












            yes agreed @Fox
            – Praveen Kumar BS
            Dec 26 '17 at 16:32




            yes agreed @Fox
            – Praveen Kumar BS
            Dec 26 '17 at 16:32












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f413038%2fretrieve-first-occurrence-of-record-where-matching-pattern-is-taken-from-input%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)