regex - How to extract the last name in an array of a full name? -
suppose have full name in bash array, want robustly extract last name , non last name (the first name , middle name if exists). example, show following 3 examples indicate complexity of problem.
x1=(john von neumann) x2=(michael jeffrey jordan) x3=(michael jordan)
does have way extract last name , non last name? thanks.
i'm assuming put every single name in separate array. more flexible way use regular expression. in plain english regex says: - either last name starts lowercase char followed number of alphabetic characters , spaces - or last name follows last space in string.
take @ @ this:
#!/bin/bash x1=(john von neumann) x2=(michael jeffrey jordan) x3=(michael jordan) x4=(charles-jean etienne gustave nicholas de la vallée-poussin) regex="[[:space:]]([a-z]+.*|[a-z][^[:space:]]+)$" in 1 2 3 4 eval name=\${"x"$i[@]} if [[ $name =~ $regex ]]; fullname=${bash_rematch[1]} echo $fullname fi done
Comments
Post a Comment