Printing unique lines











up vote
10
down vote

favorite
2












Is there some better solution for printing unique lines other than a combination of sort and uniq?










share|improve this question


















  • 1




    What do you mean by "better"?
    – gabe.
    Mar 23 '11 at 13:31










  • @gabe Not requiring the entire file to be stored in memory for example.
    – Let_Me_Be
    Mar 23 '11 at 13:46















up vote
10
down vote

favorite
2












Is there some better solution for printing unique lines other than a combination of sort and uniq?










share|improve this question


















  • 1




    What do you mean by "better"?
    – gabe.
    Mar 23 '11 at 13:31










  • @gabe Not requiring the entire file to be stored in memory for example.
    – Let_Me_Be
    Mar 23 '11 at 13:46













up vote
10
down vote

favorite
2









up vote
10
down vote

favorite
2






2





Is there some better solution for printing unique lines other than a combination of sort and uniq?










share|improve this question













Is there some better solution for printing unique lines other than a combination of sort and uniq?







command-line text-processing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 '11 at 22:29









Let_Me_Be

4,39973257




4,39973257








  • 1




    What do you mean by "better"?
    – gabe.
    Mar 23 '11 at 13:31










  • @gabe Not requiring the entire file to be stored in memory for example.
    – Let_Me_Be
    Mar 23 '11 at 13:46














  • 1




    What do you mean by "better"?
    – gabe.
    Mar 23 '11 at 13:31










  • @gabe Not requiring the entire file to be stored in memory for example.
    – Let_Me_Be
    Mar 23 '11 at 13:46








1




1




What do you mean by "better"?
– gabe.
Mar 23 '11 at 13:31




What do you mean by "better"?
– gabe.
Mar 23 '11 at 13:31












@gabe Not requiring the entire file to be stored in memory for example.
– Let_Me_Be
Mar 23 '11 at 13:46




@gabe Not requiring the entire file to be stored in memory for example.
– Let_Me_Be
Mar 23 '11 at 13:46










4 Answers
4






active

oldest

votes

















up vote
19
down vote



accepted










To print each identical line only one, in any order:



sort -u


To print only the unique lines, in any order:



sort | uniq -u


To print each identical line only once, in the order of their first occurrence: (for each line, print the line if it hasn't been seen yet, then in any case increment the seen counter)



awk '!seen[$0] {print}
{++seen[$0]}'


To print only the unique lines, in the order of their first occurrence: (record each line in seen, and also in lines if it's the first occurrence; at the end of the input, print the lines in order of occurrence but only the ones seen only once)



awk '!seen[$0]++ {lines[i++]=$0}
END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}'





share|improve this answer

















  • 7




    how about awk '!seen[$0]++ {print}'?
    – asoundmove
    Mar 23 '11 at 3:26






  • 8




    Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
    – quazgar
    Jun 4 '15 at 10:23




















up vote
2
down vote













Some (most?) versions of sort have a -u flag that does the uniq part directly. Might be some line length restrictions depending on the implementation though, but you had those already with plain sort|uniq.






share|improve this answer



















  • 1




    Er? sort -u goes back to V7 at least.
    – geekosaur
    Mar 22 '11 at 22:46










  • Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
    – Mat
    Mar 22 '11 at 22:50










  • Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
    – geekosaur
    Mar 22 '11 at 22:52










  • @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
    – Jonathan Leffler
    Mar 23 '11 at 3:32


















up vote
0
down vote













Does Perl work for you? It can keep the lines in the original order, even if the duplicates are not adjacent. You could also code it in Python, or awk.



while (<>) {
print if $lines{$_}++ == 0;
}


Which can be shortened to just



perl -ne 'print unless $lines{$_}++;'


Given input file:



abc
def
abc
ghi
abc
def
abc
ghi
jkl


It yields the output:



abc
def
ghi
jkl





share|improve this answer























  • Where is $lines getting defined?
    – Gregg Leventhal
    Jul 20 '14 at 3:09










  • It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
    – Jonathan Leffler
    Jul 20 '14 at 4:47










  • I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
    – Kusalananda
    Apr 6 '17 at 8:29


















up vote
0
down vote













For the last part of the answer mentioned in : Printing unique lines by @Gilles as an answer to this question, I tried to eliminate the need for using two hashes.



This solution is for : To print only the unique lines, in the order of their first occurrence:



awk '{counter[$0]++}
END {for (line in counter) if (counter[line]==1) print line}'



Here, "counter" stores a count of each line that is similar to the one processed earlier.

At the end, we print only those lines, that have counter value as 1.






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%2f9918%2fprinting-unique-lines%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    19
    down vote



    accepted










    To print each identical line only one, in any order:



    sort -u


    To print only the unique lines, in any order:



    sort | uniq -u


    To print each identical line only once, in the order of their first occurrence: (for each line, print the line if it hasn't been seen yet, then in any case increment the seen counter)



    awk '!seen[$0] {print}
    {++seen[$0]}'


    To print only the unique lines, in the order of their first occurrence: (record each line in seen, and also in lines if it's the first occurrence; at the end of the input, print the lines in order of occurrence but only the ones seen only once)



    awk '!seen[$0]++ {lines[i++]=$0}
    END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}'





    share|improve this answer

















    • 7




      how about awk '!seen[$0]++ {print}'?
      – asoundmove
      Mar 23 '11 at 3:26






    • 8




      Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
      – quazgar
      Jun 4 '15 at 10:23

















    up vote
    19
    down vote



    accepted










    To print each identical line only one, in any order:



    sort -u


    To print only the unique lines, in any order:



    sort | uniq -u


    To print each identical line only once, in the order of their first occurrence: (for each line, print the line if it hasn't been seen yet, then in any case increment the seen counter)



    awk '!seen[$0] {print}
    {++seen[$0]}'


    To print only the unique lines, in the order of their first occurrence: (record each line in seen, and also in lines if it's the first occurrence; at the end of the input, print the lines in order of occurrence but only the ones seen only once)



    awk '!seen[$0]++ {lines[i++]=$0}
    END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}'





    share|improve this answer

















    • 7




      how about awk '!seen[$0]++ {print}'?
      – asoundmove
      Mar 23 '11 at 3:26






    • 8




      Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
      – quazgar
      Jun 4 '15 at 10:23















    up vote
    19
    down vote



    accepted







    up vote
    19
    down vote



    accepted






    To print each identical line only one, in any order:



    sort -u


    To print only the unique lines, in any order:



    sort | uniq -u


    To print each identical line only once, in the order of their first occurrence: (for each line, print the line if it hasn't been seen yet, then in any case increment the seen counter)



    awk '!seen[$0] {print}
    {++seen[$0]}'


    To print only the unique lines, in the order of their first occurrence: (record each line in seen, and also in lines if it's the first occurrence; at the end of the input, print the lines in order of occurrence but only the ones seen only once)



    awk '!seen[$0]++ {lines[i++]=$0}
    END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}'





    share|improve this answer












    To print each identical line only one, in any order:



    sort -u


    To print only the unique lines, in any order:



    sort | uniq -u


    To print each identical line only once, in the order of their first occurrence: (for each line, print the line if it hasn't been seen yet, then in any case increment the seen counter)



    awk '!seen[$0] {print}
    {++seen[$0]}'


    To print only the unique lines, in the order of their first occurrence: (record each line in seen, and also in lines if it's the first occurrence; at the end of the input, print the lines in order of occurrence but only the ones seen only once)



    awk '!seen[$0]++ {lines[i++]=$0}
    END {for (i in lines) if (seen[lines[i]]==1) print lines[i]}'






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 22 '11 at 23:06









    Gilles

    524k12610481578




    524k12610481578








    • 7




      how about awk '!seen[$0]++ {print}'?
      – asoundmove
      Mar 23 '11 at 3:26






    • 8




      Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
      – quazgar
      Jun 4 '15 at 10:23
















    • 7




      how about awk '!seen[$0]++ {print}'?
      – asoundmove
      Mar 23 '11 at 3:26






    • 8




      Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
      – quazgar
      Jun 4 '15 at 10:23










    7




    7




    how about awk '!seen[$0]++ {print}'?
    – asoundmove
    Mar 23 '11 at 3:26




    how about awk '!seen[$0]++ {print}'?
    – asoundmove
    Mar 23 '11 at 3:26




    8




    8




    Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
    – quazgar
    Jun 4 '15 at 10:23






    Or even shorter awk '!seen[$0]++', since the {print} is implied by an empty command.
    – quazgar
    Jun 4 '15 at 10:23














    up vote
    2
    down vote













    Some (most?) versions of sort have a -u flag that does the uniq part directly. Might be some line length restrictions depending on the implementation though, but you had those already with plain sort|uniq.






    share|improve this answer



















    • 1




      Er? sort -u goes back to V7 at least.
      – geekosaur
      Mar 22 '11 at 22:46










    • Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
      – Mat
      Mar 22 '11 at 22:50










    • Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
      – geekosaur
      Mar 22 '11 at 22:52










    • @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
      – Jonathan Leffler
      Mar 23 '11 at 3:32















    up vote
    2
    down vote













    Some (most?) versions of sort have a -u flag that does the uniq part directly. Might be some line length restrictions depending on the implementation though, but you had those already with plain sort|uniq.






    share|improve this answer



















    • 1




      Er? sort -u goes back to V7 at least.
      – geekosaur
      Mar 22 '11 at 22:46










    • Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
      – Mat
      Mar 22 '11 at 22:50










    • Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
      – geekosaur
      Mar 22 '11 at 22:52










    • @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
      – Jonathan Leffler
      Mar 23 '11 at 3:32













    up vote
    2
    down vote










    up vote
    2
    down vote









    Some (most?) versions of sort have a -u flag that does the uniq part directly. Might be some line length restrictions depending on the implementation though, but you had those already with plain sort|uniq.






    share|improve this answer














    Some (most?) versions of sort have a -u flag that does the uniq part directly. Might be some line length restrictions depending on the implementation though, but you had those already with plain sort|uniq.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 22 '11 at 22:54

























    answered Mar 22 '11 at 22:42









    Mat

    38.7k8117125




    38.7k8117125








    • 1




      Er? sort -u goes back to V7 at least.
      – geekosaur
      Mar 22 '11 at 22:46










    • Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
      – Mat
      Mar 22 '11 at 22:50










    • Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
      – geekosaur
      Mar 22 '11 at 22:52










    • @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
      – Jonathan Leffler
      Mar 23 '11 at 3:32














    • 1




      Er? sort -u goes back to V7 at least.
      – geekosaur
      Mar 22 '11 at 22:46










    • Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
      – Mat
      Mar 22 '11 at 22:50










    • Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
      – geekosaur
      Mar 22 '11 at 22:52










    • @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
      – Jonathan Leffler
      Mar 23 '11 at 3:32








    1




    1




    Er? sort -u goes back to V7 at least.
    – geekosaur
    Mar 22 '11 at 22:46




    Er? sort -u goes back to V7 at least.
    – geekosaur
    Mar 22 '11 at 22:46












    Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
    – Mat
    Mar 22 '11 at 22:50




    Hum... I thought I remembered Solaris or AIX not having that. I'm wrong though, they both have it.
    – Mat
    Mar 22 '11 at 22:50












    Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
    – geekosaur
    Mar 22 '11 at 22:52




    Solaris and AIX have -u but also have a 512-character line length restriction. (Actually, I think somewhere around Solaris 9 Sun upped it to 5120. GNU still wins, though.)
    – geekosaur
    Mar 22 '11 at 22:52












    @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
    – Jonathan Leffler
    Mar 23 '11 at 3:32




    @geekosaur: are you sure? The work done to remove the 512-byte limit on line length in sort was documented in 'Theory and Practice in the Construction of a Working Sort Routine' by J P Linderman, Bell System Technical. Journal, 63, 1827- 1843 (1984).
    – Jonathan Leffler
    Mar 23 '11 at 3:32










    up vote
    0
    down vote













    Does Perl work for you? It can keep the lines in the original order, even if the duplicates are not adjacent. You could also code it in Python, or awk.



    while (<>) {
    print if $lines{$_}++ == 0;
    }


    Which can be shortened to just



    perl -ne 'print unless $lines{$_}++;'


    Given input file:



    abc
    def
    abc
    ghi
    abc
    def
    abc
    ghi
    jkl


    It yields the output:



    abc
    def
    ghi
    jkl





    share|improve this answer























    • Where is $lines getting defined?
      – Gregg Leventhal
      Jul 20 '14 at 3:09










    • It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
      – Jonathan Leffler
      Jul 20 '14 at 4:47










    • I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
      – Kusalananda
      Apr 6 '17 at 8:29















    up vote
    0
    down vote













    Does Perl work for you? It can keep the lines in the original order, even if the duplicates are not adjacent. You could also code it in Python, or awk.



    while (<>) {
    print if $lines{$_}++ == 0;
    }


    Which can be shortened to just



    perl -ne 'print unless $lines{$_}++;'


    Given input file:



    abc
    def
    abc
    ghi
    abc
    def
    abc
    ghi
    jkl


    It yields the output:



    abc
    def
    ghi
    jkl





    share|improve this answer























    • Where is $lines getting defined?
      – Gregg Leventhal
      Jul 20 '14 at 3:09










    • It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
      – Jonathan Leffler
      Jul 20 '14 at 4:47










    • I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
      – Kusalananda
      Apr 6 '17 at 8:29













    up vote
    0
    down vote










    up vote
    0
    down vote









    Does Perl work for you? It can keep the lines in the original order, even if the duplicates are not adjacent. You could also code it in Python, or awk.



    while (<>) {
    print if $lines{$_}++ == 0;
    }


    Which can be shortened to just



    perl -ne 'print unless $lines{$_}++;'


    Given input file:



    abc
    def
    abc
    ghi
    abc
    def
    abc
    ghi
    jkl


    It yields the output:



    abc
    def
    ghi
    jkl





    share|improve this answer














    Does Perl work for you? It can keep the lines in the original order, even if the duplicates are not adjacent. You could also code it in Python, or awk.



    while (<>) {
    print if $lines{$_}++ == 0;
    }


    Which can be shortened to just



    perl -ne 'print unless $lines{$_}++;'


    Given input file:



    abc
    def
    abc
    ghi
    abc
    def
    abc
    ghi
    jkl


    It yields the output:



    abc
    def
    ghi
    jkl






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 6 '17 at 8:07


























    community wiki





    3 revs, 2 users 96%
    Jonathan Leffler













    • Where is $lines getting defined?
      – Gregg Leventhal
      Jul 20 '14 at 3:09










    • It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
      – Jonathan Leffler
      Jul 20 '14 at 4:47










    • I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
      – Kusalananda
      Apr 6 '17 at 8:29


















    • Where is $lines getting defined?
      – Gregg Leventhal
      Jul 20 '14 at 3:09










    • It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
      – Jonathan Leffler
      Jul 20 '14 at 4:47










    • I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
      – Kusalananda
      Apr 6 '17 at 8:29
















    Where is $lines getting defined?
    – Gregg Leventhal
    Jul 20 '14 at 3:09




    Where is $lines getting defined?
    – Gregg Leventhal
    Jul 20 '14 at 3:09












    It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
    – Jonathan Leffler
    Jul 20 '14 at 4:47




    It isn't. Since there isn't a use strict; or use warnings; (actually, it is strict that is most relevant here), there is no complaint about using %lines before it is defined. If run with strictures, there'd need to be a line my %lines; before the loop. Note, too, that the hash is %lines; one element of the hash is referenced using the $lines{$_} notation.
    – Jonathan Leffler
    Jul 20 '14 at 4:47












    I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
    – Kusalananda
    Apr 6 '17 at 8:29




    I think the sort solutions may be better for large amount of data (the OP was concerned about "storing the entire file in memory"). sort will perform an out-of-core sort if the data is larger than the available memory.
    – Kusalananda
    Apr 6 '17 at 8:29










    up vote
    0
    down vote













    For the last part of the answer mentioned in : Printing unique lines by @Gilles as an answer to this question, I tried to eliminate the need for using two hashes.



    This solution is for : To print only the unique lines, in the order of their first occurrence:



    awk '{counter[$0]++}
    END {for (line in counter) if (counter[line]==1) print line}'



    Here, "counter" stores a count of each line that is similar to the one processed earlier.

    At the end, we print only those lines, that have counter value as 1.






    share|improve this answer

























      up vote
      0
      down vote













      For the last part of the answer mentioned in : Printing unique lines by @Gilles as an answer to this question, I tried to eliminate the need for using two hashes.



      This solution is for : To print only the unique lines, in the order of their first occurrence:



      awk '{counter[$0]++}
      END {for (line in counter) if (counter[line]==1) print line}'



      Here, "counter" stores a count of each line that is similar to the one processed earlier.

      At the end, we print only those lines, that have counter value as 1.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        For the last part of the answer mentioned in : Printing unique lines by @Gilles as an answer to this question, I tried to eliminate the need for using two hashes.



        This solution is for : To print only the unique lines, in the order of their first occurrence:



        awk '{counter[$0]++}
        END {for (line in counter) if (counter[line]==1) print line}'



        Here, "counter" stores a count of each line that is similar to the one processed earlier.

        At the end, we print only those lines, that have counter value as 1.






        share|improve this answer












        For the last part of the answer mentioned in : Printing unique lines by @Gilles as an answer to this question, I tried to eliminate the need for using two hashes.



        This solution is for : To print only the unique lines, in the order of their first occurrence:



        awk '{counter[$0]++}
        END {for (line in counter) if (counter[line]==1) print line}'



        Here, "counter" stores a count of each line that is similar to the one processed earlier.

        At the end, we print only those lines, that have counter value as 1.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 2 '17 at 16:18









        Sarfraaz Ahmed

        111




        111






























            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.





            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%2funix.stackexchange.com%2fquestions%2f9918%2fprinting-unique-lines%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