0

A sample array I have like below;

OVGARR=(ACCS ELB NOVOVG SCN MISOVG)

The position of the strings with *OVG can be in any index and there are more than different types of OVG strings. I want to get the value of the 1st *OVG string on the array, cut the "OVG" part of the string and assign it to a variable.

My initial solution is like below;

OV=`printf '%s\n' "${OVGARR[@]}" | grep -P 'OVG$' | head -1 | awk -F'OVG' '{print $1}'`

Though it works, is there a shorter and more efficient approach to what I would like to do?

2 Answers 2

1

Based on your original awk solution, but stripping the need to use grep and head:

 OV="$(awk -F 'OVG' '/OVG/ { print $1;exit }' <(printf '%s\n' "${OVGARR[@]}"))"
Sign up to request clarification or add additional context in comments.

1 Comment

This solution do appear to be more simple than what I have. I will try this. Thank you Raman.
0

Not shelling out seems to be much faster. How easy it is to read is subjective.

for o in "${OVGARR[@]}" ; do
    if [[ $o == *OVG ]] ; then
        OV=${o%OVG}
        break
    fi
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.