I have an array of strings called names containing names with some subsequent garbage data. Like this
Jill Shortz, City Contractor, America
Bill Torts, Family Doctor, Canada
Will Courtz, Folk DJ, Bulgaria
Phil-Lip Warts, Juggler, India
I want to iterate through names extracting only the first two words with the regex (^\w+-*( *\w+)*) and overwriting them back into names so it will contain
Jill Shortz
Bill Torts
Will Courtz
Phil-Lip Warts
this is how I attempted it but my AIX machine does not like the -P argument to execute in Perl mode
for((i=0;i<${#names[@]};++i)); do
names[$i]=`grep -P '(^\w+-*( *\w+)*)' -o <<<"${names[i]}"`
done
names[$i]="${names[$i]%%,*}"^[^,]*(a - possibly empty - sequence of non-,characters, anchored to the start)