Extracting a Number from a String
Quote:
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:
|
Quote:
|
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 Code:
# python -c "print '\n'.join([i.strip().split(',')[-2] for i in open('file')])" |
Quote:
Code:
echo 'Alison Moyet,All Cried Out,410' | perl -e '$_=<STDIN>;m/(\d+)/; print $1, "\n"' |
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. |
Quote:
|
Quote:
|
echo 'Alison Moyet,All Cried Out,410' | grep -Eo "[[:digit:]]*"
410 |
note that sample data has ending ","
|
Shell:
Code:
string="The Angels,Take A Long Line,180," |
Quote:
Try this Code:
echo 'Alison Moyet,All Cried Out,410,ad,333,' | grep -Eo "[[:digit:]]*" |
Quote:
|
Quote:
Code:
# echo 'Alison Moyet,All Cried Out,410,ad,333,' | grep -Eo "[[:digit:]]+" |
Quote:
Code:
#!/usr/bin/env python |
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:
|
All times are GMT -5. The time now is 11:50 PM. |