Grep a number from a line in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a number from a line in ksh
# 1  
Old 12-09-2007
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  
Old 12-09-2007
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  
Old 12-09-2007
Quote:
Originally Posted by prowla
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  
Old 12-09-2007
Quote:
Originally Posted by rodluo
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).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

GREP function in ksh which ignores LINE Breaks

Hello I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from... (24 Replies)
Discussion started by: Raghav Garg
24 Replies

2. How to Post in the The UNIX and Linux Forums

GREP function in ksh which ignores LINE Breaks

I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command - grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from this,... (3 Replies)
Discussion started by: Raghav Garg
3 Replies

3. Shell Programming and Scripting

Grep line btn Number

HI Guys, I want grep Below line from a huge file. 01 01 -094.6 -093.3 -095.8 -094.0 01.3/01.3 18.1/18.7 I want to grep line which have data between -60 to -200 Thanks (4 Replies)
Discussion started by: pareshkp
4 Replies

4. Shell Programming and Scripting

Retrieving line number from grep

Hi. im trying to retrieve the line number from grep. i have 1 part of my code here. grep -n $tgt file.txt | cut -f 1 -d ":" when i do not cut the value i have is 12:aaa:abc:aaa:aaa:aaa how can i store the value of 12 or my whole line of string into a variable with grep? (6 Replies)
Discussion started by: One_2_three
6 Replies

5. UNIX for Dummies Questions & Answers

how to grep a number from output line

I`m having a output shown below, CFR 235,BBC DM-2 ALL CFR 111,BBC DM-2 ALL CFR 333,BBC DM-2 ALL from the above Output i want to use 235,111,333 as input for other purpose. these no always change every time i run script.so please suggest me the way i could do it with example,i have tried... (5 Replies)
Discussion started by: nitin_aaa27
5 Replies

6. UNIX for Dummies Questions & Answers

Line number of an occurrence using grep

Hi All, is there a way to extract the line number of an occurrence using grep? I know that with the -n option it prints out the line number as well. I would like to assign the line number to a variable. Thanks, Sarah (5 Replies)
Discussion started by: f_o_555
5 Replies

7. Shell Programming and Scripting

finding the line number from a grep ?

Hi there does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file test1 test2 test3 test1 and i needed to get the line numbers for all instances of test1 in that file with the answer being (1,4) Would anybody be able... (7 Replies)
Discussion started by: hcclnoodles
7 Replies

8. Shell Programming and Scripting

Using grep to extract line number

I'm trying to use grep to get the line number only. This is the command I'm using: grep -n "Content-Disposition: attachment" mbox The output I get is: 45:Content-Disposition: attachment; filename="test.txt" So now I just want to get the line number (45) from this output. Can someone... (8 Replies)
Discussion started by: mskarica
8 Replies

9. UNIX for Dummies Questions & Answers

Get Filename and Line Number using grep

Hi, I am using the Korne shell to try and get the filename, line number and the line of text using grep e.g. find ./ -type f -name "*.java" -exec grep -nf test.txt '{}' \; (test.txt contains strings to search) will return the line number and the line of text. grep -l would return the... (4 Replies)
Discussion started by: ceemh3
4 Replies

10. Shell Programming and Scripting

obtaining a line number in ksh programming

Hi People, how do i obtain the line number of a ksh shell script that i am working on For example in C,when u key in __LINE__ will return u the current line number of the src code. regards wilson (1 Reply)
Discussion started by: wilsontan
1 Replies
Login or Register to Ask a Question