Extract a string from a file and store it in variable

Hi,

I've a file ImageSizeDetails.txt with the following contents:

Image Name:   swncd 01.10.00.04
Created:      Wed Jan  9 14:05:48 2013
Image Type:   ARM Linux Multi-File Image (gzip compressed)
Data Size:    7351011 Bytes = 7178.72 kB = 7.01 MB
Load Address: 00008000
Entry Point:  00008000
Contents:
   Image 0: 1672879 Bytes = 1633.67 kB = 1.60 MB
   Image 1: 5678119 Bytes = 5545.04 kB = 5.42 MB

I need to extract the size of Image 0 in bytes (i.e) 1672879 alone& save it in a variable. Kindly help me with a shell script

One way:

isiz=$(awk '/Image 0/{print $3}' file)

Guru

Pls use code tags as advised!
You may want to learn awk , as it is a very powerful tool for requirements like your. For a starting help, try

$ var=$(awk '/Image 0/{print $3}' file); echo $var
1672879

Thank you, so much

Hi

Also you can try following two codes

grep -w "Image 0" ImageSizeDetails.txt|awk '{print $3}'
grep -w "Image 0" ImageSizeDetails.txt|cut -d " " -f3

i dont think, second grep command will work.

 
$ cat a.txt
   Image 0: 1672879 Bytes = 1633.67 kB = 1.60 MB
$ grep -w "Image 0" a.txt | cut -d " " -f3
 
$ grep -w "Image 0" a.txt | cut -d " " -f5
0:
 
$ grep -w "Image 0" a.txt | cut -d " " -f6
1672879

Hi Kamaraj,

It was my mistake.
You are right......:slight_smile:
It should be

grep -w "Image 0" a.txt | cut -d " " -f6