Suppose I have a file called names.txt, which contains a list of names and corresponding emails of people in the following format:

FName1 LName1 <random1@test.com>
FName2 LName2 <random2@test2.com>
FName3 LName3 <random3@test3.com>
FName4 <random1@some.com>
FName5 MName1 LName4 <something@test34.com>
FName1 LName1 <random01@asdf.com>

What I am trying to accomplish is to output all of the unique people, case-insensitively, based on name (i.e. ignoring email) from names.txt. So, the output would look like the following:

FName1 LName1
FName2 LName2 
FName3 LName3 
FName5 MName1 LName4  

Note that the same name can appear multiple times in the file and names can be a mix of lower case characters, upper case characters, numbers, etc. Examples of names that can appear would be: "JoHn sMitH JOnes", "StEve", or "RoB3rt Fro5t"

What I am struggling with is how to just output the unique names without the associated email. I am able to use grep to match the patterns I am looking to output by using the following:

grep -i "^[A-Za-z0-9]*[ ]*[A-Za-z0-9]*[ ]*[A-Za-z0-9]*" names.txt

However, I am unsure how to use these pattern results and output the unique names while leaving out the email part of each line. Is there some way I can use these grep results and send them as standard input into some other command? Any feedback or suggestion would be greatly appreciated.


With awk:

awk '
    sub(/<.*/, "") # remove email address
    $1 = $1 # remove leading and trailing blanks, squeeze all sequences of blanks
            # into one space
    if (!seen[tolower($0)]++) print # print if not seen before
  }' < names.txt

You can go the other way around. Ignore the emails:

cut -d'<' -f1 names.txt | sort -fu

That cuts the line at the < separator, and prints the first field, sorts (ignoring the case -f) and then keeps just the first one of names that only differ in their case (also ignoring the case with -u).

  • Yes @StéphaneChazelas, you're right. For some reason I tend to "spell out" that kind of pipes, even when I work with them myself. Added to the answer. – Eduardo Trápani May 18 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.