我有一個電子郵件地址,我想將電子郵件拆分為名字的第一個字母和全名,并將它們放在一個占位符中。例如:名字.姓氏@email.com
x = f
y = lastname
我確實嘗試firstCharacter=${email:0:1}獲取第一個字符,但不確定如何獲取姓氏。
完整代碼在這里
#!/bin/bash
file="users.csv.1"
Count=0
while IFS=";"; read email birthDate group sharedFolder
do
echo -e "Email: $email"
firstCharacter=${email:0:1}
echo $firstCharacter
IFS="."
read -ra
echo -e "Birth Date: $birthDate"
echo -e "Group: $group"
echo -e "shared Folder: $sharedFolder\n"
done < "$file"
uj5u.com熱心網友回復:
local_part=${email%%@*} # remove trailing `@*` (glob-style pattern)
last_name=${local_part##*.} # remove the leading `*.` from above
uj5u.com熱心網友回復:
使用引數擴展/替換的一個想法:
$ email='[email protected]'
$ lastName="${email//@*}" # strip off domain (ie, everything from @ to end of string)
$ lastName="${lastName//*.}" # strip off everything from start of string up to to and including last period; should address email addresses like 'firstname.mi.lastname'
$ typeset -p lastName
declare -- lastName="lastname"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/507865.html
