I'm trying to add the .data and .bss sections of the size -A output

size -A mylib.so | grep "\.data" | awk '{print $2}

this will print the value of the size of the .data section.

I guess I could copy / paste this command inside awk, but this will get ugly fast.

Is there a way to parse several lines like this at once?

  • size -A mylib.so | awk '/\.data/{printf ("%s ",$2)}END{print ""}'
    – Costas
    Nov 3, 2014 at 20:20

1 Answer 1


Is this what you want?

size -A mylib.so | 
awk '$1 == ".data" || $1 == ".bss" {sum += $2} END {print sum}'

If you just want to extract the .data and .bss sizes from the size output, and if you have GNU grep:

size -A mylib.so | grep -oP '^(\.data|\.bss)\s+\K\d+'
  • so awk does parse each line separately by default?
    – Eric
    Nov 3, 2014 at 20:25
  • yes, awk is a "record" oriented tool. By default, the record separator is a newline, meaning each line is a record. Nov 3, 2014 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .