|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
One way: Code:
isiz=$(awk '/Image 0/{print $3}' file)Guru |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
Thank you, so much
|
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
|
||||
|
||||
|
Quote:
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
|
||||
|
||||
|
Hi Kamaraj,
It was my mistake. You are right...... ![]() It should be grep -w "Image 0" a.txt | cut -d " " -f6 |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|