Find the answer to your Linux question:
Results 1 to 7 of 7
I have a file that has many lines. But I want to print only unique lines in that file. i.e. I dont want to print similar lines. How to do ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2006
    Posts
    4

    how to print unique lines from a file?


    I have a file that has many lines. But I want to print only unique lines in that file. i.e. I dont want to print similar lines. How to do it?

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,807
    Use grep.

    You can take each line from a file, use grep to count the matches in that same file, and if the number found is only 1, echo the line to the display.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Sep 2006
    Posts
    4

    Question help ?????

    but suppose there are 5 similar lines in the file then I want to print only one of the 5 lines.? pls help

  4. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,807
    Errrr, forgive me for not understanding what you're saying, but...

    If there are 5 similar lines in the file (and similar means 'nearly the same' not 'unique') then what you asked in the first place was for something that will print them all...

    Perhaps you could mackle some kind of bash algorthim that checked each character in the line with each other character, and if maybe 80% match up, then you could treat them as the same? Sounds quite tricky though. 'Nearly the same' or 'approximately equal to' is quite hard to do in computer terms.
    Linux user #126863 - see http://linuxcounter.net/

  5. #5
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Code:
    sort <file> | uniq
    sort sorts, uniq removes similar consecutive lines. That'll work fine if you don't mind the order of the lines being changed.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  6. #6
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,393
    Another option is to use "sort -u" and then redirect the output to lpr or another file e.g.
    Code:
    #sort -u somefile | lpr
    or
    Code:
    #sort -u somefile >> someotherfile

  7. #7
    Just Joined!
    Join Date
    Dec 2010
    Posts
    1
    I have a qmail server that every now again gets filled up by my users, so to find out which email campaign is breaking it I run the following.

    ./qmHandle -R | grep Subject | sort | uniq > subjects.txt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •