How to extract a number from a statement?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to extract a number from a statement?
# 1  
Old 03-07-2014
How to extract a number from a statement?

Hi,

i want to extract a number from a statement.

03/07/14 00:58:41 CRPr::CopyTotable inserted 3501 rows into table

Now i want to assign 3501 to a variable. This number may change every time.
# 2  
Old 03-07-2014
Code:
num=$( echo '03/07/14 00:58:41 CRPr::CopyTotable inserted 3501 rows into table ' | awk ' { print $5 } ' )

# 3  
Old 03-07-2014
Hi anbu23,

I am putting your statement in a function.
It doesnot work if i put your statement in a function because i am using $5 after awk statement. It tries to find the argument present in 5th positional parameter of the function.
Please suggest another way to substitue $5.
# 4  
Old 03-07-2014
If you have GNU sed, then this will work:

Code:
num=$(echo "03/07/14 00:58:41 CRPr::CopyTotable inserted 3501 rows into table" | sed -r 's|.*inserted (.*?) rows.*|\1|')

echo $num
3501

If that still doesn't work, please post your function.
# 5  
Old 03-07-2014
Are you saying that you can't be sure that it will be position 5?

If you have the value as a variable, you can splice the string up if you are certain that the value you want is the field before the word rows:-
Code:
variable="03/07/14 00:58:41 CRPr::CopyTotable inserted 3501 rows into table"

var="${variable% rows*}"            # Trim off everything after <space> rows
var="${var##* }"                    # Trim off everything before the final space

echo $var

This way will process faster than calling sed



Robin

Last edited by rbatte1; 03-07-2014 at 09:48 AM.. Reason: Added comments to code
# 6  
Old 03-08-2014
Code:
echo '...' | cut -d" " -f5

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract number from column

I have a lot of file with a lot of lines following the same pattern the lines go like this: alpha_9/output- -413.74928476 2.6116 and I want it to be: 9 -413.74928476 2.6116 thanks for the help (5 Replies)
Discussion started by: galboski
5 Replies

2. Shell Programming and Scripting

Extract number from string.

Hi I am on Sun os. I have data in the below format and I need to grab the number out from the string. O/p needed: ---------- Post updated at 12:39 PM ---------- Previous update was at 12:32 PM ---------- I tried this but I am getting . at the front (14 Replies)
Discussion started by: dsravanam
14 Replies

3. Shell Programming and Scripting

If statement test against number range [0-9]

Is it possible to test against a varible within a ranges in a if statement. ex. if ];then echo "not in range" else echo "number within range" fi (8 Replies)
Discussion started by: leemalloy
8 Replies

4. Shell Programming and Scripting

How to extract port number?

Hi my code is as follow: stringA=`cat /s01/oracle/11.2/network/admin/listener.ora | grep "PORT"` stringB=`cat $ORACLE_HOME/network/admin/listener.ora | grep "PORT"` stringC="PORT" echo ${stringA} echo ${stringB} echo ${stringC} position=`expr index "$stringB" "stringC"` echo... (2 Replies)
Discussion started by: jediwannabe
2 Replies

5. Shell Programming and Scripting

Extract Number

I am trying to extract the numbers from the strings. Lakers win 80% of the games 24 numbered Kobe scores 90% from free throw line Chances of Lakers winning championship is 100% I have data like this. and am looking to extract the % 80% 90% 100% (6 Replies)
Discussion started by: grajp002
6 Replies

6. Shell Programming and Scripting

Extract number that comes after a given string

Hi! I have a file that contains non-regular strings like: SCSGTI:N="$4,0,1,4,34622991111-->RemoteSPC: 1111", NWID=1; SCSGTI:N="$4,0,1,4,34622991211-->RemoteSPC: 1211", NWID=1; SCSGTI:N="$4,0,1,4,*-->RemoteSPC: 2112,Sec:RemoteSPC: 2212", NWID=1; SCSGTI:N="$4,10,1,4,34622999213-->RemoteSPC:... (4 Replies)
Discussion started by: Flavius
4 Replies

7. Shell Programming and Scripting

Extract specific case statement entries

I've got a script with one massive case statement consisting of about 130 entries. Each entry essentially passes variables into a compiled executable. Is it possible to extract the code in a specific statement? Each case entry is unique :( My goal is to dynamically create a flat file to... (4 Replies)
Discussion started by: ironhalo
4 Replies

8. Shell Programming and Scripting

Extract the highest number out

Hi Gurus, I've using HPUX B.11.23 U ia64 with shell = sh. I've been having some problem get the highest number of this script. Actually I wanted to get the highest number from this listing (TEST123 data and based on this highest number, there will be email being sent out. For example,... (6 Replies)
Discussion started by: superHonda123
6 Replies

9. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

10. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question