LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Extracting a Number from a String (https://www.linuxquestions.org/questions/programming-9/extracting-a-number-from-a-string-728542/)

nilly16 05-26-2009 04:29 AM

Extracting a Number from a String
 
Quote:

The Angels,Take A Long Line,180,
How do I take this string of text & extract & store the number 180 in an array?

What functions do I use? For example cin.ignore(array, ",")

I have a whole text file where I have to extract just the numbers from each string.
Quote:

Alison Moyet,All Cried Out,410,
Allan Browne Quintet,Cyclosporin,291,
The Angels,Take A Long Line,180,
Architecture In Helsinki,Maybe You Can Owe Me,242,

Sergei Steshenko 05-26-2009 04:44 AM

Quote:

Originally Posted by nilly16 (Post 3552907)
How do I take this string of text & extract & store the number 180 in an array?

What functions do I use? For example cin.ignore(array, ",")

I have a whole text file where I have to extract just the numbers from each string.

What computer programming language are you talking about ?

ghostdog74 05-26-2009 05:26 AM

most probably C++
@OP, i assume you are not doing homework and are free to use any programming language you might have
Code:

# awk -F"," '{print $(NF-1)}' file
410
291
180
242

if you have Python
Code:

# python -c "print '\n'.join([i.strip().split(',')[-2] for i in open('file')])"
410
291
180
242


Sergei Steshenko 05-26-2009 06:09 AM

Quote:

Originally Posted by ghostdog74 (Post 3552958)
most probably C++
@OP, i assume you are not doing homework and are free to use any programming language you might have
Code:

# awk -F"," '{print $(NF-1)}' file
410
291
180
242

if you have Python
Code:

# python -c "print '\n'.join([i.strip().split(',')[-2] for i in open('file')])"
410
291
180
242


Huh ?

Code:

echo 'Alison Moyet,All Cried Out,410' | perl -e '$_=<STDIN>;m/(\d+)/; print $1, "\n"'
410


syg00 05-26-2009 06:16 AM

Much as I appreciate perl, this just screams for grep.
Now, if there are an indeterminate number of digit (sub)strings, that might change my leaning.

Sergei Steshenko 05-26-2009 06:17 AM

Quote:

Originally Posted by syg00 (Post 3552995)
Much as I appreciate perl, this just screams for grep.
Now, if there are an indeterminate number of digit (sub)strings, that might change my leaning.

Does 'grep' extract numbers ? I think it just matches, and the OP needs extraction.

ghostdog74 05-26-2009 06:19 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3552988)
Huh ?

what?

syg00 05-26-2009 06:21 AM

echo 'Alison Moyet,All Cried Out,410' | grep -Eo "[[:digit:]]*"
410

ghostdog74 05-26-2009 06:23 AM

note that sample data has ending ","

colucix 05-26-2009 06:25 AM

Shell:
Code:

string="The Angels,Take A Long Line,180,"
string=${string%,}
number=${string##*,}


syg00 05-26-2009 06:28 AM

Quote:

Originally Posted by ghostdog74 (Post 3553004)
note that sample data has ending ","

So ???.
Try this
Code:

echo 'Alison Moyet,All Cried Out,410,ad,333,' | grep -Eo "[[:digit:]]*"

Sergei Steshenko 05-26-2009 06:29 AM

Quote:

Originally Posted by ghostdog74 (Post 3552998)
what?

Too much complexity in your Python code.

ghostdog74 05-26-2009 06:31 AM

Quote:

Originally Posted by syg00 (Post 3553011)
So ???.
Try this
Code:

echo 'Alison Moyet,All Cried Out,410,ad,333,' | grep -Eo "[[:digit:]]*"

Code:

# echo 'Alison Moyet,All Cried Out,410,ad,333,' | grep -Eo "[[:digit:]]+"
410
333


ghostdog74 05-26-2009 06:38 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3553012)
Too much complexity in your Python code.

Code:

#!/usr/bin/env python
for line in open('file')
    print line.strip().split(',')[-2]

(NB. "complexity" is subjective)

pixellany 05-26-2009 06:38 AM

nilly;

This thread is your third one on what appears to be the same homework assignment. These threads should have been kept together---or at least you should have referenced them. It's a little late, but I've included the canned response for homework threads.

Quote:

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
I'm reporting all of your threads for closure.


All times are GMT -5. The time now is 11:50 PM.