字符串中的唯一电子邮件

时间:2016-09-13 13:35:37

标签: string bash unique

我有一个包含电子邮件的字符串,有些是重复的。仅举例:

"aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com"

我希望字符串只包含唯一的电子邮件,逗号分隔。结果应该是:

"aaa@company.com,bbb@company.com,ccc@company.com"

这么简单吗?

P.S。电子邮件各不相同,我不知道它们会包含什么。

4 个答案:

答案 0 :(得分:4)

这个怎么样:

 <?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $address = $_POST['address'];
    $ip = $_SERVER['REMOTE_ADDR'];
    if($name !=''&& $email !=''&& $contact !=''&& $address !='')
    {
        $curl_handle=curl_init();
        curl_setopt($curl_handle,CURLOPT_URL,'http://myexample.com');
        curl_setopt($curl_handle, CURLOPT_POST, 1);
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "name=$_POST['name']&email=$_POST['email']&contact=$_POST['contact']&address=$_POST['address']");
        $res = curl_exec($curl_handle);
        curl_close($curl_handle);
        if ($res) {
            echo "success message";
        }
    }
    else{
        ?><span><?php echo "You must fill out all the fields";?></span> <?php
    }
}`enter code here`
?>

我将分隔逗号转换为换行符,以便我可以使用与行一起使用的工具(如echo "aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com" | tr ',' '\n' | sort | uniq | tr '\n' ',' | sed -e 's/,$//' sortuniq)。

答案 1 :(得分:1)

仅使用awkprocess-substitution使用sort和其他工具。

awk  -vORS="," '!seen[$1]++' < <(echo "aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com" |   tr ',' '\n') 
aaa@company.com,bbb@company.com,ccc@company.com

或者使用pure-bash并完全避免tr的另一种方法是

# Read into a bash array with field-separator as ',' read with '-a' for reading to an array
IFS=',' read -ra myArray <<< "aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com"


# Printing the array elements new line and feeding it to awk
awk  -vORS="," '!seen[$1]++' < <(printf '%s\n' "${myArray[@]}")
aaa@company.com,bbb@company.com,ccc@company.com

答案 2 :(得分:0)

使用perl

$ s="aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com"
$ echo $s | perl -MList::MoreUtils=uniq -F, -le 'print join ",",uniq(@F)'
aaa@company.com,bbb@company.com,ccc@company.com

答案 3 :(得分:0)

获取数组中的字符串:

IFS=','; read -r -a lst <<< "aaa@company.com,bbb@company.com,aaa@company.com,bbb@company.com,ccc@company.com"

排序和过滤:

IFS=$'\n' sort <<< "${lst[*]}" | uniq