combine like terms in bash

2019-09-07 10:58发布

I have a list of domain names in a text file with a number of times they occur in a collection of email files. For example:

 598 aol.com
  1 aOL.COM
  4 Aol.com
  1 AOl.com
  6 AOL.com
 39 AOL.COM

There were 598 emails sent to aol.com and 1 sent to aOL.COM and so on. I was wondering if there was a way in bash to combine aol.com and aOL.COM and all the other aliases since they are in fact the same thing. Any help would be greatly appreciated!

This is the line of code that produced that output:

grep -E -o -r "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" $ARCHIVE | sed 's/.*@//' | sort | uniq -c > temp2

2条回答
走好不送
2楼-- · 2019-09-07 11:54

Add a -i (--ignore-case) flag to the uniq command in your one-liner:

grep -E -o -r "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" $ARCHIVE \
    | sed 's/.*@//' \
    | sort \
    | uniq -ic > temp2

From the uniq man page:

-i
--ignore-case
    Ignore differences in case when comparing lines.
查看更多
我命由我不由天
3楼-- · 2019-09-07 11:54

I would recommend changing the program producing this code to first make everything lowercase, (Converting string to lower case in Bash shell scripting), then try sorting.

Doing this after the fact would just make your life harder.

查看更多
登录 后发表回答