LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2013, 05:02 PM   #1
5883
Member
 
Registered: Aug 2004
Posts: 173

Rep: Reputation: 0
How to find the first decimal number in a string ?


looking for a simple 1 line solution,

e.g.
tmpfs 200 5 195 2% /tmp

it should return 200.

thanks !
 
Old 08-08-2013, 05:24 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This is one way ...

With this InFile ...
Code:
tmpfs 200 5 195 2% /tmp
151 367 how now brown cow
ab98def four five six 789 happy
... this awk ...
Code:
awk '{for (j=1;j<=NF;j++) if ($j+0==$j) {print $j; break}}' $InFile >$OutFile
... produced this OutFile ...
Code:
200
151
789
Daniel B. Martin
 
1 members found this post helpful.
Old 08-08-2013, 06:18 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Note that Mr. Martin's solution will pick out the first field that parses to any valid number, not just integer numbers.
Code:
$ cat inFile 
tmpfs 200 5 195 2% /tmp
151 367 how now brown cow
ab98def four five six 789 happy
1e5 7 19
one two 3.14159 e
1.4test 2.4

$ awk '{for (j=1;j<=NF;j++) if ($j+0==$j) {print $j; break}}' inFile
200
151
789
1e5
3.14159
2.4
 
1 members found this post helpful.
Old 08-08-2013, 06:48 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by PTrenholme View Post
Note that Mr. Martin's solution will pick out the first field that parses to any valid number, not just integer numbers.
Quite right. OP asked for "the first decimal number." The definition of decimal number includes 200, 151, 789, 1e5, 3.14159, 2.4, and many others.

Daniel B. Martin
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert number (not hex) into Decimal number drManhattan Programming 10 10-15-2011 08:53 PM
i need to split a decimal number in such a way that each unit is between 1 and 26 anurupr Programming 8 03-07-2010 10:36 PM
How to find which file has a string when large number of files dwynter Linux - Newbie 2 09-10-2008 06:06 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM
c: find out if a string represents a number? kpachopoulos Programming 2 08-15-2006 02:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration