Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello.

Here is a file contents :

declare -Ax NEW_FORCE_IGNORE_ARRAY=([FORCE_IGNORE15]="§" [FORCE_IGNORE17]="§" [FORCE_IGNORE1]="§" [FORCE_IGNORE16]="§" [FORCE_IGNORE11]="§" .................. [FORCE_IGNORE23]="§"

Here is a pattern

=

I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '='

Is there a better solution than mine :

MY_PATTERN="="
SOME_VAR=$(cat $SOME_FILE | awk -F$MY_PATTERN '{print $1}' |  awk '{print $3}' )
echo $SOME_VAR

Any help is welcome

Hello jcdole,

Yes, that could be shorten to following.

MY_PATTERN="="
SOME_VAR=$(awk -F"$MY_PATTERN" '{split($1,array," ");print array[3]}' Input_file)
echo "$SOME_VAR"

Thanks,
R. Singh

1 Like

Using built-in parameter substitution:-

while read line
do
        SOME_VAR="${line%%=*}"
        print "${SOME_VAR##* }"
done < Input_file
1 Like

Thank you every body for helping