Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Extract a string from a file and store it in variable

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-24-2013
Parameswaran Parameswaran is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 10 April 2013, 3:12 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Extract a string from a file and store it in variable

Hi,

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


Code:
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

Last edited by Scrutinizer; 01-24-2013 at 03:54 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 01-24-2013
guruprasadpr's Unix or Linux Image
guruprasadpr guruprasadpr is offline Forum Advisor  
Shrink...ing
 
Join Date: Jun 2009
Last Activity: 1 December 2014, 8:24 AM EST
Location: India
Posts: 676
Thanks: 30
Thanked 215 Times in 214 Posts
One way:


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

Guru
Sponsored Links
    #3  
Old Unix and Linux 01-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 5 May 2015, 5:30 AM EDT
Location: Aachen, Germany
Posts: 5,825
Thanks: 103
Thanked 1,547 Times in 1,454 Posts
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
Code:
$ var=$(awk '/Image 0/{print $3}' file); echo $var
1672879

    #4  
Old Unix and Linux 01-24-2013
Parameswaran Parameswaran is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 10 April 2013, 3:12 AM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you, so much
Sponsored Links
    #5  
Old Unix and Linux 01-24-2013
sdebasis's Unix or Linux Image
sdebasis sdebasis is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 6 April 2015, 6:42 AM EDT
Posts: 23
Thanks: 10
Thanked 2 Times in 2 Posts
Hi

Also you can try following two codes


Code:
grep -w "Image 0" ImageSizeDetails.txt|awk '{print $3}'


Code:
grep -w "Image 0" ImageSizeDetails.txt|cut -d " " -f3


Last edited by Scrutinizer; 01-24-2013 at 04:05 AM.. Reason: ICODE tags changed to CODE tags; again: ICODE tags changed to CODE tags
Sponsored Links
    #6  
Old Unix and Linux 01-24-2013
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 4 April 2015, 12:03 PM EDT
Posts: 3,060
Thanks: 34
Thanked 660 Times in 639 Posts
Quote:
Originally Posted by sdebasis View Post
Hi

Also you can try following two codes


Code:
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.


Code:
 
$ 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

Sponsored Links
    #7  
Old Unix and Linux 01-24-2013
sdebasis's Unix or Linux Image
sdebasis sdebasis is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 6 April 2015, 6:42 AM EDT
Posts: 23
Thanks: 10
Thanked 2 Times in 2 Posts
Hi Kamaraj,

It was my mistake.
You are right......Unix or Linux Image
It should be

grep -w "Image 0" a.txt | cut -d " " -f6
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search for string in a file, extract two another strings and concatenate to a variable garg Shell Programming and Scripting 6 06-21-2012 10:37 AM
Search file for string and store last result to variable gwr Shell Programming and Scripting 5 01-30-2011 06:47 AM
Search for string in a file and extract another string to a variable daikeyang Shell Programming and Scripting 6 03-20-2009 08:45 PM
Using GREP/AWK to extract a number and store it as a variable modey3 UNIX for Dummies Questions & Answers 3 03-06-2009 02:36 PM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 04:22 AM



All times are GMT -4. The time now is 09:16 AM.