Extract Number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract Number
# 1  
Old 01-27-2011
Extract Number

I am trying to extract the numbers from the strings.

Code:
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 %

Code:
80%
90%
100%

# 2  
Old 01-27-2011
Try this,
Code:
grep -o "[0-9]*%"  inputfile

OR
Code:
awk '{for(i=1;i<=NF;i++){if($i ~/%/) print $i}}' inputfile

# 3  
Old 01-27-2011
One way with sed..
Code:
sed 's/.* \([^ ]*%\).*/\1/' inptutfile > outfile

# 4  
Old 01-27-2011
bash can:
Code:
while read -r line; do [[ $line =~ ([[:digit:]]*%) ]] && echo "${BASH_REMATCH[1]}"; done < file

# 5  
Old 01-27-2011
Another approach with sed:
Code:
sed 's/.* \([0-9]*\)%.*/\1%/' file

# 6  
Old 01-27-2011
A Perl :
Code:
perl -ne ' @a=m/(\d+%)/g;foreach (@a){print $_."\n"}' file

# 7  
Old 01-27-2011
Code:
grep -Ewo "[0-9]+%" infile

Code:
tr ' ' '\n' < infile | grep -xE "[0-9]+%"

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. UNIX for Dummies Questions & Answers

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. (5 Replies)
Discussion started by: arghadeep adity
5 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 particular number from the command output

Hi Folks, I want to use particular number as a variable output..Please find the below for scenario... Example 1:- Below output i want to use secondary group 9003 as a variable output $ id -a |awk -NF '{print $3}' groups=99(local),9003(testadmin) Else I want to use 2006 as a... (8 Replies)
Discussion started by: susindram
8 Replies

6. Shell Programming and Scripting

how to extract last word and a number from a string

I have the following script (which I made by my self) #!/bin/bash # add a few empty lines to make it more legible # add a date description on each update interval echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >>... (6 Replies)
Discussion started by: mahirzukic2
6 Replies

7. 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

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

How to extract the number from the pattern

Hi I have a file with the following entries: cm2363db cm2445db cm2618db Is there a awk or sed command which would allow me to extract only the numbers from this file as output, so the output would look like this: 2363 2445 2618 Thanks a lot -A (4 Replies)
Discussion started by: aoussenko
4 Replies

10. Shell Programming and Scripting

How can i extract month number ?

I wanted to extract the month number from unix. unixdev(invent) /home/odsapp/apps/CTSI ==> date Tue Nov 11 14:42:27 EST 2008 From date command, i can get day(11), year(2008). But i need month number.. I am receiving files as test11102008.txt My need to identify when this file is... (1 Reply)
Discussion started by: govindts
1 Replies
Login or Register to Ask a Question