1

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 <[email protected]>
FName2 LName2 <[email protected]>
FName3 LName3 <[email protected]>
FName4 <[email protected]>
FName5 MName1 LName4 <[email protected]>
FName1 LName1 <[email protected]>
...

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 
FName4 
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.

2 Answers 2

3

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
1

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).

1
  • 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. Commented May 18, 2021 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .