Extract a string from a file and store it in variable | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Extract a string from a file and store it in variable

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 01-24-2013
guruprasadpr's Avatar
guruprasadpr guruprasadpr is offline Forum Advisor  
Shrink...ing
 
Join Date: Jun 2009
Last Activity: 15 April 2014, 5:07 AM EDT
Location: India
Posts: 675
Thanks: 30
Thanked 215 Times in 214 Posts
One way:


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

Guru
Sponsored Links
    #3  
Old 01-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 September 2014, 3:14 PM EDT
Location: Aachen, Germany
Posts: 4,273
Thanks: 72
Thanked 1,031 Times in 977 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 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 01-24-2013
sdebasis's Avatar
sdebasis sdebasis is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 July 2014, 4:58 AM EDT
Posts: 23
Thanks: 9
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 01-24-2013
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 8 August 2014, 4:56 AM EDT
Posts: 3,057
Thanks: 33
Thanked 658 Times in 637 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 01-24-2013
sdebasis's Avatar
sdebasis sdebasis is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 24 July 2014, 4:58 AM EDT
Posts: 23
Thanks: 9
Thanked 2 Times in 2 Posts
Hi Kamaraj,

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

grep -w "Image 0" a.txt | cut -d " " -f6
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:13 AM.