The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using grep to extract line number mskarica Shell Programming and Scripting 8 06-26-2008 03:47 AM
How to grep / zgrep to output ONLY the matching filename and line number? vvaidyan UNIX for Dummies Questions & Answers 3 03-12-2008 05:33 PM
grep the string with the line number salaathi Shell Programming and Scripting 4 12-04-2007 11:42 AM
Get Filename and Line Number using grep ceemh3 UNIX for Dummies Questions & Answers 4 09-17-2007 11:35 AM
is there any why to get the number of line in grep result ? umen UNIX for Dummies Questions & Answers 1 03-16-2006 09:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-09-2007
rodluo rodluo is offline
Registered User
  
 

Join Date: Dec 2007
Location: Hong Kong
Posts: 2
Grep a number from a line in ksh

In file.name, I have a line that reads

$IDIR/imgen -usemonths -dropcheck -monitor -sizelimit 80000000 -interval 120 -volcal HSI

How can I get the size limit, i.e. 80000000 out and pass it to a variable called SIZE?

Thanks. I tried
echo "grep sizelimit file.name" | sed -n -e 's/^.sizelimit\([0-9]*\) -interval.*$/\1/p'

but it output nothing.
  #2 (permalink)  
Old 12-09-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
So close...


Code:
SIZE=`grep sizelimit myfile | sed 's/^.*sizelimit //' | awk '{print $1}'`

should work (NB. It allows for the arguments to be in a different order in the file).
There are other options that involve parsing the line (either in awk or shell), like:


Code:
SIZE=0
grep sizelimit myfile | while read s
do
  set -- $s
  while [ $# -gt 0 ]
  do
    case $1 in
      -sizelimit) SIZE=$2; break;;
      *) shift;;
    esac
  done
done

  #3 (permalink)  
Old 12-09-2007
rodluo rodluo is offline
Registered User
  
 

Join Date: Dec 2007
Location: Hong Kong
Posts: 2
Quote:
Originally Posted by prowla View Post
So close...


Code:
SIZE=`grep sizelimit myfile | sed 's/^.*sizelimit //' | awk '{print $1}'`

should work (NB. It allows for the arguments to be in a different order in the file).
There are other options that involve parsing the line (either in awk or shell), like:


Code:
SIZE=0
grep sizelimit myfile | while read s
do
  set -- $s
  while [ $# -gt 0 ]
  do
    case $1 in
      -sizelimit) SIZE=$2; break;;
      *) shift;;
    esac
  done
done
Thanks a lot prowla!
The first option works fine. Did you mean it works even if "-sizelimit 80000000" is moved somewhere else in the line too?
  #4 (permalink)  
Old 12-09-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
Quote:
Originally Posted by rodluo View Post
Thanks a lot prowla!
The first option works fine. Did you mean it works even if "-sizelimit 80000000" is moved somewhere else in the line too?
No probs, and yes - the line can change just so long as -sizelimit is followed by its value.
What it actually does is chops everything from the start of the line up to and including the word sizelimit and the space after, and then takes the first word from the remainder (which is the value of sizelimit).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0