I have the below block of data.

Heap after GC invocations=31 (full 3):  
 par new generation   total 131008K, used 0K [0x00000000eac00000, 0x00000000f2c00000, 0x00000000f2c00000)  
  eden space 130944K,   0% used [0x00000000eac00000, 0x00000000eac00000,   0x00000000f2be0000)
  from space 64K,   0% used [0x00000000f2be0000, 0x00000000f2be0000, 0x00000000f2bf0000)  
  to   space 64K,   0% used [0x00000000f2bf0000, 0x00000000f2bf0000, 0x00000000f2c00000)  
 concurrent mark-sweep generation total 131072K, used 48549K [0x00000000f2c00000, 0x00000000fac00000, 0x00000000fac00000)  
 concurrent-mark-sweep perm gen total 30000K, used 19518K [0x00000000fac00000, 0x00000000fc94c000, 0x0000000100000000)  

I need to extract the below data of "total" and "used" numeric data without "K" for the below. i.e. value1=131008, value2=0,value3=131072,value4=48549,value5=30000 and value6=19518

should be extracted from the below:

par new generation   ***total*** 131008K, ***used*** 0K
concurrent mark-sweep generation ***total*** 131072K, used 48549K 
concurrent-mark-sweep perm gen ***total*** 30000K, ***used*** 19518K

I know how to extract the data for fixed length values like below.

value1=`grep "par new generation" | cut -c27-31

However, the above block of data has variable length values.

1 Answer 1


If your goal is to extract those six numbers into shell variables, it is probably more convenient to put them in a bash array like this:

$ data=($(awk '/^ *(par|concurrent)/{printf "%s %s ",$5+0,$7+0}' file))

You can verify that the array has the correct values using declare:

$ declare -p data
declare -a data='([0]="131008" [1]="0" [2]="131072" [3]="48549" [4]="30000" [5]="19518")'

If, instead, you just want to print the values:

$ awk '/^ *(par|concurrent)/{printf "value%s=%s\nvalue%s=%s\n",++c,$5+0,++c,$7+0}' file

How is works

  • /^ *(par|concurrent)/

    This matches only on the lines that start with par or concurrent.

  • printf "%s %s ",$5+0,$7+0

    For the matched lines, we print out the fifth and seventh fields. By adding zero to these values, we force awk to convert them to numbers. This has the side-effect of removing the k.

  • Thank you so much. I did not expect such quick resolution.
    – alex
    Commented Jun 25, 2015 at 22:43

You must log in to answer this question.

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