2013-12-23 46 views
-1

檢查郵件,我有一些電子郵件,其中包含一個和其他 有時甚至單一的郵件之間的空間......這是處境實例:與空間

  1. 行1 - [email protected] [email protected] [email protected] [email protected] ecc。 ECC。
  2. line2 - [email protected](沒有最後的空格) ecc.ecc。

THI是我的代碼:

regex="^[-0-9a-zA-Z.+_][email protected][-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}" 
if [[ "$mail" =~ $regex ]]; 
then 
    echo "OK $mail" 
else 
    echo "WARNING CHECK: $mail" 
fi 
done 

我怎麼在正則表達式的進入空間,而當會給我錯誤一封電子郵件?

更新2

我有逗號更新列表XLS分離

  • 行1 - FIELD1,皮波@ gmail.com,達斯@ gmail.com
  • 2號線 - FILED1,皮波@ gmail.com,darth @ gmail.com,sampei @ gmail.com

output =(awk -F','-v var =「$ awkvar」'$ 1 == var {print $ 2,$ 3,$ 4 } spreadsheet.txt)

但在awk在輸出

回聲 「$輸出」

[email protected] [email protected] 
[email protected] [email protected] [email protected] 

的開始刪除一些信件,但如果我刪除了$ 4

輸出=(AWK -F '' - v VAR = 「$ awkvar」「$ 1 ==變種{打印$ 2,$ 3} spreadsheet.txt)

回聲 「$輸出」

它是line1的正確顯示,但不是line2的:

[email protected] [email protected] 
[email protected] [email protected] 

我在做什麼錯?

+0

嗯,貴方覺得可能是爲了驗證每個電子郵件單獨 –

+0

我不明白你期望的結果是什麼標準化你的數據的最好的主意。示例文本中的所有電子郵件地址都可以嗎? –

+0

@glennjackman是的,所有的電子表格.xls – pasaico

回答

1

逐行讀取數據線成的陣列(在該行的每個字是由空格分開):

re='^[[:alnum:]_.+-][email protected][[:alnum:]_.+-]+\.[[:alnum:]]{2,4}$' 
while read -ra addresses; do 
    for addr in "${addresses[@]}"; do 
     if [[ $addr =~ $re ]]; then 
      echo "OK $addr" 
     else 
      echo "WARNING CHECK: $addr" 
     fi 
    done 
done <<END 
[email protected] [email protected] [email protected] [email protected] ecc. ecc. 
[email protected] ecc.ecc. 
[email protected] 
[email protected] 
[email protected] 
END 
OK [email protected] 
OK [email protected] 
OK [email protected] 
OK [email protected] 
WARNING CHECK: ecc. 
WARNING CHECK: ecc. 
OK [email protected] 
WARNING CHECK: ecc.ecc. 
WARNING CHECK: [email protected] 
WARNING CHECK: [email protected] 
OK [email protected] 

驗證電子郵件地址看起來比較棘手。這是一個有效的電子郵件地址:

(Glenn Jackman) "Glenn Jackman" @ some.domain.example.com 
+0

'postbox @ com'是一個有效的電子郵件地址,以及'abcdefghi @ abc.museum'。請參閱http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses – Toto

2

您可以使用egrep -o分別捕獲每個電子郵件使用相同的正則表達式,你必須:

> s='[email protected] [email protected] [email protected] [email protected]' 
> egrep -o "[-0-9a-zA-Z.+_][email protected][-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}" <<< "$s" 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

編輯:我想在這裏補充一點email addresses can contain spaces (between quotes)

+2

BTW的正則表達式將不考慮許多有效的電子郵件地址允許的地址比這個複雜得多 – Matteo

+0

當然,電子郵件地址有很多內容。我剛剛重用了OP的正則表達式來將行分解成單獨的電子郵件地址。 – anubhava

+1

我知道我只是想指出。另外:電子郵件地址可以包含空格(在引號之間)。我只會在答案中加上警告。 – Matteo