Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract exact element from array by matching pattern in unix

Tags:

bash

unix

myArray=('Prasanth' 'kumar' 'guru')

Is it possible to get element which ends with "santh" without iterating the array(myArray) with bash.

My expected output is to get Prasanth without iterating the above array, just by checking wildcard like ('santh')and get the element.

like image 883
user6543599 Avatar asked Mar 24 '26 09:03

user6543599


1 Answers

I suggest:

printf '%s\n' "${myArray[@]}" | grep 'santh$'

Output:

Prasanth
like image 61
Cyrus Avatar answered Mar 26 '26 10:03

Cyrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!