SAN ALexis SAN ALexis - 4 months ago 9
Linux Question

parsing outpout from nm command with awk - Linux Bash

I working on some code optimization and I would to inspect the size of my functions instead to read huge disassembly file. After compilation on Debug, I use the nm command to read the .o. We got this :

nm --size-sort $OBJFILEPATH.o

000000000000000b r __PRETTY_FUNCTION__.6473
0000000000000017 t extract_h
0000000000000036 t L_mult
000000000000003a t sature32
0000000000000042 t L_mac
0000000000000048 t L_add
000000000000005c t Mac16x11
0000000000000077 t L_shl
0000000000000083 t L_shr
00000000000000df T G729Convolve
0000000000000114 T G729Residu
00000000000001bc T G729Syn_filt_L_H
00000000000001bc T G729Syn_filt_L_SUBFR


Now, into a bash script I would like to parse only the 1st column wich each line represent a single array element in bash.

My command will be :

read FUNCSIZE <<< $(nm --size-sort $OBJFILEPATH.o | awk '{print $1}')


And for make sure that everything is ok I check the size of my FUNCSIZE array.

SIZE=${#FUNCSIZE[@]}
echo size is $SIZE
for s in $FUNCSIZE
do
echo $s
done


I got this as output:

size is 1
000000000000000b
0000000000000017
0000000000000036
000000000000003a
0000000000000042
0000000000000048
000000000000005c
0000000000000077
0000000000000083
00000000000000df
0000000000000114
00000000000001bc
00000000000001bc


Why size is '1' and why I'm able to print each element like it was inside an array. It's seem that the output still have a 'space' ' ' at the end of the line. Is there any RegEx with awk that will avoid to include the separator field into the array?

Thank for your help !!

EDIT AFTER ANWSER

read -a FUNCSIZE <<< $(nm --size-sort $OBJFILEPATH.o | awk '{print $1}')
SIZE=${#FUNCSIZE[*]}
for((i=0; i<SIZE; i++))
do
echo ${FUNCSIZE[$i]}
done

Answer

The way you wrote it, FUNCSIZE is not an array, but a regular variable.

Add the -a flag for read:

read -a FUNCSIZE <<< $(nm --size-sort $OBJFILEPATH.o | awk '{print $1}')

Another way to create a Bash array:

FUNCSIZE=($(nm --size-sort $OBJFILEPATH.o | awk '{print $1}'))
Comments