Remove duplicate lines from a file but leave 1 occurrenceRemove duplicate lines while keeping the order of the linesHow can I “cat” a file and remove commented lines?How can I write a sed script to delete numbers from a line?Merge labels from lines with duplicate fieldsHow can i remove duplicates from the output of tshark outputDelete all lines that contain duplicate lettersprint the duplicate lines only on fields 1,2 from csv fileremove lines in file 1 from file 2how to verify occurrence of 3 lines in filePrint non consecutive lines from file given line number listRemove specific words within lines

How to format long polynomial?

"You are your self first supporter", a more proper way to say it

I'm flying to France today and my passport expires in less than 2 months

NMaximize is not converging to a solution

Rock identification in KY

Which country benefited the most from UN Security Council vetoes?

What's the output of a record needle playing an out-of-speed record

Is it legal for company to use my work email to pretend I still work there?

What defenses are there against being summoned by the Gate spell?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

How can I make my BBEG immortal short of making them a Lich or Vampire?

Horror movie about a virus at the prom; beginning and end are stylized as a cartoon

Today is the Center

Was any UN Security Council vote triple-vetoed?

Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?

How does quantile regression compare to logistic regression with the variable split at the quantile?

How to determine what difficulty is right for the game?

Add text to same line using sed

Is it unprofessional to ask if a job posting on GlassDoor is real?

Theorems that impeded progress

What does the "remote control" for a QF-4 look like?

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?

Is it possible to do 50 km distance without any previous training?



Remove duplicate lines from a file but leave 1 occurrence


Remove duplicate lines while keeping the order of the linesHow can I “cat” a file and remove commented lines?How can I write a sed script to delete numbers from a line?Merge labels from lines with duplicate fieldsHow can i remove duplicates from the output of tshark outputDelete all lines that contain duplicate lettersprint the duplicate lines only on fields 1,2 from csv fileremove lines in file 1 from file 2how to verify occurrence of 3 lines in filePrint non consecutive lines from file given line number listRemove specific words within lines






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm looking to remove duplicate lines from a file but leave 1 occurrence in the file.



Example of the file:



this is a string
test line
test line 2
this is a string


From the above example, I would want to remove 1 occurrence of "this is a string".



Best way to do this?










share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    With such questions you should always provide example input and output.

    – Hauke Laging
    May 19 '18 at 13:12






  • 1





    Possibly related: Remove duplicate lines while keeping the order of the lines

    – steeldriver
    May 19 '18 at 13:12











  • Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

    – Kusalananda
    May 19 '18 at 13:14






  • 1





    Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

    – roaima
    May 19 '18 at 13:17






  • 1





    it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

    – peterh
    May 19 '18 at 19:03

















0















I'm looking to remove duplicate lines from a file but leave 1 occurrence in the file.



Example of the file:



this is a string
test line
test line 2
this is a string


From the above example, I would want to remove 1 occurrence of "this is a string".



Best way to do this?










share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1





    With such questions you should always provide example input and output.

    – Hauke Laging
    May 19 '18 at 13:12






  • 1





    Possibly related: Remove duplicate lines while keeping the order of the lines

    – steeldriver
    May 19 '18 at 13:12











  • Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

    – Kusalananda
    May 19 '18 at 13:14






  • 1





    Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

    – roaima
    May 19 '18 at 13:17






  • 1





    it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

    – peterh
    May 19 '18 at 19:03













0












0








0








I'm looking to remove duplicate lines from a file but leave 1 occurrence in the file.



Example of the file:



this is a string
test line
test line 2
this is a string


From the above example, I would want to remove 1 occurrence of "this is a string".



Best way to do this?










share|improve this question
















I'm looking to remove duplicate lines from a file but leave 1 occurrence in the file.



Example of the file:



this is a string
test line
test line 2
this is a string


From the above example, I would want to remove 1 occurrence of "this is a string".



Best way to do this?







linux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 19 '18 at 18:33







Tom Bailey

















asked May 19 '18 at 13:09









Tom BaileyTom Bailey

162




162





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.









  • 1





    With such questions you should always provide example input and output.

    – Hauke Laging
    May 19 '18 at 13:12






  • 1





    Possibly related: Remove duplicate lines while keeping the order of the lines

    – steeldriver
    May 19 '18 at 13:12











  • Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

    – Kusalananda
    May 19 '18 at 13:14






  • 1





    Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

    – roaima
    May 19 '18 at 13:17






  • 1





    it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

    – peterh
    May 19 '18 at 19:03












  • 1





    With such questions you should always provide example input and output.

    – Hauke Laging
    May 19 '18 at 13:12






  • 1





    Possibly related: Remove duplicate lines while keeping the order of the lines

    – steeldriver
    May 19 '18 at 13:12











  • Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

    – Kusalananda
    May 19 '18 at 13:14






  • 1





    Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

    – roaima
    May 19 '18 at 13:17






  • 1





    it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

    – peterh
    May 19 '18 at 19:03







1




1





With such questions you should always provide example input and output.

– Hauke Laging
May 19 '18 at 13:12





With such questions you should always provide example input and output.

– Hauke Laging
May 19 '18 at 13:12




1




1





Possibly related: Remove duplicate lines while keeping the order of the lines

– steeldriver
May 19 '18 at 13:12





Possibly related: Remove duplicate lines while keeping the order of the lines

– steeldriver
May 19 '18 at 13:12













Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

– Kusalananda
May 19 '18 at 13:14





Are the duplicated lines adjacent to one another? Is the output to remain in the same order or would it be ok to sort the data?

– Kusalananda
May 19 '18 at 13:14




1




1





Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

– roaima
May 19 '18 at 13:17





Keep one occurrence of a duplicate (ie two identical lines per match) or simply "remove all duplicate lines, leaving only one line per set of duplicates"? Does the final order matter?

– roaima
May 19 '18 at 13:17




1




1





it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

– peterh
May 19 '18 at 19:03





it is not a problem for you that the lines will be sorted, then a sort file|uniq will do what you want.

– peterh
May 19 '18 at 19:03










2 Answers
2






active

oldest

votes


















1














Demo file stuff.txt contains:



one
two
three
one
two
four
five


Remove duplicate lines from a file assuming you don't mind that lines are sorted



$ sort -u stuff.txt 
five
four
one
three
two


Explanation: the u flag sent to sort says sort the lines of the file and force unique.



Remove duplicate lines from a file, preserve original ordering, keep the first:



$ cat -n stuff.txt | sort -uk2 | sort -nk1 | cut -f2-
one
two
three
four
five


Explanation: The n flag passed to cat appends line numbers to left of every line, plus space, then the first sort says sort by unique and but only after the first word, the second sort command says use the line numbers we stored in step 1 to resort by the original ordering, finally cut off the first word.



Remove duplicate lines from a file, preserve order, keep last.



tac stuff.txt > stuff2.txt; cat -n stuff2.txt | sort -uk2 | sort -nk1 | cut -f2- > stuff3.txt; tac stuff3.txt > stuff4.txt; cat stuff4.txt
three
one
two
four
five


Explanation: Same as before, but tac reverse the file, achieving the desired result.






share|improve this answer






























    0














    This leaves the first occurrence:



    awk '! a[$0]++' inputfile

    start cmd:> echo 'this is a string
    cont. cmd:> test line
    cont. cmd:> test line 2
    cont. cmd:> this is a string' | awk '! a[$0]++'
    this is a string
    test line
    test line 2





    share|improve this answer

























    • It seems to just print out and not actually make in changes in the file.

      – Tom Bailey
      May 19 '18 at 15:49











    • @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

      – Hauke Laging
      May 19 '18 at 16:49












    • I have edited it now.

      – Tom Bailey
      May 19 '18 at 19:29











    • @TomBailey works fine for me.

      – Hauke Laging
      May 19 '18 at 20:16











    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%2f444795%2fremove-duplicate-lines-from-a-file-but-leave-1-occurrence%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









    1














    Demo file stuff.txt contains:



    one
    two
    three
    one
    two
    four
    five


    Remove duplicate lines from a file assuming you don't mind that lines are sorted



    $ sort -u stuff.txt 
    five
    four
    one
    three
    two


    Explanation: the u flag sent to sort says sort the lines of the file and force unique.



    Remove duplicate lines from a file, preserve original ordering, keep the first:



    $ cat -n stuff.txt | sort -uk2 | sort -nk1 | cut -f2-
    one
    two
    three
    four
    five


    Explanation: The n flag passed to cat appends line numbers to left of every line, plus space, then the first sort says sort by unique and but only after the first word, the second sort command says use the line numbers we stored in step 1 to resort by the original ordering, finally cut off the first word.



    Remove duplicate lines from a file, preserve order, keep last.



    tac stuff.txt > stuff2.txt; cat -n stuff2.txt | sort -uk2 | sort -nk1 | cut -f2- > stuff3.txt; tac stuff3.txt > stuff4.txt; cat stuff4.txt
    three
    one
    two
    four
    five


    Explanation: Same as before, but tac reverse the file, achieving the desired result.






    share|improve this answer



























      1














      Demo file stuff.txt contains:



      one
      two
      three
      one
      two
      four
      five


      Remove duplicate lines from a file assuming you don't mind that lines are sorted



      $ sort -u stuff.txt 
      five
      four
      one
      three
      two


      Explanation: the u flag sent to sort says sort the lines of the file and force unique.



      Remove duplicate lines from a file, preserve original ordering, keep the first:



      $ cat -n stuff.txt | sort -uk2 | sort -nk1 | cut -f2-
      one
      two
      three
      four
      five


      Explanation: The n flag passed to cat appends line numbers to left of every line, plus space, then the first sort says sort by unique and but only after the first word, the second sort command says use the line numbers we stored in step 1 to resort by the original ordering, finally cut off the first word.



      Remove duplicate lines from a file, preserve order, keep last.



      tac stuff.txt > stuff2.txt; cat -n stuff2.txt | sort -uk2 | sort -nk1 | cut -f2- > stuff3.txt; tac stuff3.txt > stuff4.txt; cat stuff4.txt
      three
      one
      two
      four
      five


      Explanation: Same as before, but tac reverse the file, achieving the desired result.






      share|improve this answer

























        1












        1








        1







        Demo file stuff.txt contains:



        one
        two
        three
        one
        two
        four
        five


        Remove duplicate lines from a file assuming you don't mind that lines are sorted



        $ sort -u stuff.txt 
        five
        four
        one
        three
        two


        Explanation: the u flag sent to sort says sort the lines of the file and force unique.



        Remove duplicate lines from a file, preserve original ordering, keep the first:



        $ cat -n stuff.txt | sort -uk2 | sort -nk1 | cut -f2-
        one
        two
        three
        four
        five


        Explanation: The n flag passed to cat appends line numbers to left of every line, plus space, then the first sort says sort by unique and but only after the first word, the second sort command says use the line numbers we stored in step 1 to resort by the original ordering, finally cut off the first word.



        Remove duplicate lines from a file, preserve order, keep last.



        tac stuff.txt > stuff2.txt; cat -n stuff2.txt | sort -uk2 | sort -nk1 | cut -f2- > stuff3.txt; tac stuff3.txt > stuff4.txt; cat stuff4.txt
        three
        one
        two
        four
        five


        Explanation: Same as before, but tac reverse the file, achieving the desired result.






        share|improve this answer













        Demo file stuff.txt contains:



        one
        two
        three
        one
        two
        four
        five


        Remove duplicate lines from a file assuming you don't mind that lines are sorted



        $ sort -u stuff.txt 
        five
        four
        one
        three
        two


        Explanation: the u flag sent to sort says sort the lines of the file and force unique.



        Remove duplicate lines from a file, preserve original ordering, keep the first:



        $ cat -n stuff.txt | sort -uk2 | sort -nk1 | cut -f2-
        one
        two
        three
        four
        five


        Explanation: The n flag passed to cat appends line numbers to left of every line, plus space, then the first sort says sort by unique and but only after the first word, the second sort command says use the line numbers we stored in step 1 to resort by the original ordering, finally cut off the first word.



        Remove duplicate lines from a file, preserve order, keep last.



        tac stuff.txt > stuff2.txt; cat -n stuff2.txt | sort -uk2 | sort -nk1 | cut -f2- > stuff3.txt; tac stuff3.txt > stuff4.txt; cat stuff4.txt
        three
        one
        two
        four
        five


        Explanation: Same as before, but tac reverse the file, achieving the desired result.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 3 at 5:33









        Eric LeschinskiEric Leschinski

        1,35711416




        1,35711416























            0














            This leaves the first occurrence:



            awk '! a[$0]++' inputfile

            start cmd:> echo 'this is a string
            cont. cmd:> test line
            cont. cmd:> test line 2
            cont. cmd:> this is a string' | awk '! a[$0]++'
            this is a string
            test line
            test line 2





            share|improve this answer

























            • It seems to just print out and not actually make in changes in the file.

              – Tom Bailey
              May 19 '18 at 15:49











            • @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

              – Hauke Laging
              May 19 '18 at 16:49












            • I have edited it now.

              – Tom Bailey
              May 19 '18 at 19:29











            • @TomBailey works fine for me.

              – Hauke Laging
              May 19 '18 at 20:16















            0














            This leaves the first occurrence:



            awk '! a[$0]++' inputfile

            start cmd:> echo 'this is a string
            cont. cmd:> test line
            cont. cmd:> test line 2
            cont. cmd:> this is a string' | awk '! a[$0]++'
            this is a string
            test line
            test line 2





            share|improve this answer

























            • It seems to just print out and not actually make in changes in the file.

              – Tom Bailey
              May 19 '18 at 15:49











            • @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

              – Hauke Laging
              May 19 '18 at 16:49












            • I have edited it now.

              – Tom Bailey
              May 19 '18 at 19:29











            • @TomBailey works fine for me.

              – Hauke Laging
              May 19 '18 at 20:16













            0












            0








            0







            This leaves the first occurrence:



            awk '! a[$0]++' inputfile

            start cmd:> echo 'this is a string
            cont. cmd:> test line
            cont. cmd:> test line 2
            cont. cmd:> this is a string' | awk '! a[$0]++'
            this is a string
            test line
            test line 2





            share|improve this answer















            This leaves the first occurrence:



            awk '! a[$0]++' inputfile

            start cmd:> echo 'this is a string
            cont. cmd:> test line
            cont. cmd:> test line 2
            cont. cmd:> this is a string' | awk '! a[$0]++'
            this is a string
            test line
            test line 2






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 19 '18 at 20:16

























            answered May 19 '18 at 13:16









            Hauke LagingHauke Laging

            57.7k1287137




            57.7k1287137












            • It seems to just print out and not actually make in changes in the file.

              – Tom Bailey
              May 19 '18 at 15:49











            • @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

              – Hauke Laging
              May 19 '18 at 16:49












            • I have edited it now.

              – Tom Bailey
              May 19 '18 at 19:29











            • @TomBailey works fine for me.

              – Hauke Laging
              May 19 '18 at 20:16

















            • It seems to just print out and not actually make in changes in the file.

              – Tom Bailey
              May 19 '18 at 15:49











            • @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

              – Hauke Laging
              May 19 '18 at 16:49












            • I have edited it now.

              – Tom Bailey
              May 19 '18 at 19:29











            • @TomBailey works fine for me.

              – Hauke Laging
              May 19 '18 at 20:16
















            It seems to just print out and not actually make in changes in the file.

            – Tom Bailey
            May 19 '18 at 15:49





            It seems to just print out and not actually make in changes in the file.

            – Tom Bailey
            May 19 '18 at 15:49













            @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

            – Hauke Laging
            May 19 '18 at 16:49






            @TomBailey That's why I told you to provide example input and output. I did test it and it works fine for me.

            – Hauke Laging
            May 19 '18 at 16:49














            I have edited it now.

            – Tom Bailey
            May 19 '18 at 19:29





            I have edited it now.

            – Tom Bailey
            May 19 '18 at 19:29













            @TomBailey works fine for me.

            – Hauke Laging
            May 19 '18 at 20:16





            @TomBailey works fine for me.

            – Hauke Laging
            May 19 '18 at 20:16

















            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%2f444795%2fremove-duplicate-lines-from-a-file-but-leave-1-occurrence%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

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            NetworkManager fails with “Could not find source connection”Trouble connecting to VPN using network-manager, while command line worksHow can I be notified about state changes to a VPN adapterBacktrack 5 R3 - Refuses to connect to VPNFeed all traffic through OpenVPN for a specific network namespace onlyRun daemon on startup in Debian once openvpn connection establishedpfsense tcp connection between openvpn and lan is brokenInternet connection problem with web browsers onlyWhy does NetworkManager explicitly support tun/tap devices?Browser issues with VPNTwo IP addresses assigned to the same network card - OpenVPN issues?Cannot connect to WiFi with nmcli, although secrets are provided