Convert string number to a integer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert string number to a integer
# 1  
Old 01-27-2017
Convert string number to a integer

I have data as below

Code:
"ROWS merge process complete.  thousand rows changed"

I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000.

Any help or pointer.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-27-2017 at 12:09 PM.. Reason: Added CODE tags.
# 2  
Old 01-27-2017
What if it were "one thousand and one"? And, which strange application / program would give an output like the quoted?

I recall to have seen an string - number converter in these fora some time (a year?) ago - try a search.
# 3  
Old 01-27-2017
Yes, I need to convert that to 10001 if that is the case.
# 4  
Old 01-27-2017
Well, in fact it was almost 2 years ago, and it was number to string. You might want to use this as a starting point and/or a source of brilliant ideas:
How to convert number to english?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-28-2017
Hi.

The reverse of that thread would be English to number.

A DIY start is Lingua::EN::Words2Nums - convert English text to numbers - metacpan.org

Good luck ... cheers, drl

---------- Post updated Jan 28th, 2017 at 08:23 ---------- Previous update was Jan 27th, 2017 at 11:55 ----------

Hi.

Here is a sample, adapted from the module documentation:
Code:
#!/usr/bin/env perl

# @(#) p2       Demonstrate conversion of English to numbers, perl module.

use strict;
use warnings;
use Lingua::EN::Words2Nums;

my ( $num, $e, $f );
print "\n";

$e = "two thousand and one";
$num = words2nums($e);
print " $e = $num\n\n";

$e   = "ROWS merge process complete.  thousand rows changed";
$num = words2nums($e);
print " $e = $num\n\n" if defined $num;

$e   = "twenty-second";
$num = words2nums($e);
print " $e = $num\n\n" if defined $num;

$e   = "15 billion, 6 million, and nineteen";
$num = words2nums($e);
$f   = commify($num) if defined $num;
print " $e = $num, commified = $f\n" if defined $num;

exit(0);

sub commify {
  my $number = shift;

  # 1 while $number =~ s/(\d+)(\d\d\d)/$1,$2/;
  1 while $number =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
  return $number;
}

producing:
Code:
$ ./p2

 two thousand and one = 2001

 twenty-second = 22

 15 billion, 6 million, and nineteen = 15006000019, commified = 15,006,000,019

On a system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
perl 5.20.2

Note that the OP text ROWS merge process complete. thousand rows changed did not produce output, because there were text strings that were not understood by the module.

One would need to extract the strings of interests, possibly by parsing the line, collecting strings, testing sequences, and, finally accepting the conversion.

Good luck ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 01-28-2017
Excellent! One question: Wouldn't "Twenty-second" require a trailing ordinal point / dot?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-28-2017
Hi, RudiC.
Quote:
Excellent! One question: Wouldn't "Twenty-second" require a trailing ordinal point / dot?
If I were doing the code, I think I'd add an option for suffixes, e.g. twenty-second -> 22nd for ordinals.

This code is available in repositories, as well as at Lingua::EN::Words2Nums - convert English text to numbers - metacpan.org . It seems to be about 10 years old, but may be amenable to updates. I also didn't dig very deeply -- this seemed to work -- but there may be other modules, perl, python, etc., that can do the same.

A brief look at the module seems like much of the work is table-driven, so it may be just a bit of work to add another table to optionally add suffixes.

There are always things of interest that come up in searches at the repo at cpan.org.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a String to a Number

I read in two numbers from a user but the number is a string. #!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 I know you can use the the "expr" or "bc" command to automatically convert the string to a number then add them together. But I don't want to add... (10 Replies)
Discussion started by: Loc
10 Replies

2. Shell Programming and Scripting

How to read from file and convert from string to integer?

Hi all, I am trying to write a script to be able to Run top command Pick the PIDs that are taking more than 90% of CPU time Get more details like what is the script name and location for that PID Script should run until I manually kill it by ctrl + C I have come up with following script... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

How to convert string into integer in shell scripting?

Hi All, sessionid_remote=$(echo "select odb_sessionid from sysopendb where odb_dbname='syscdr';" | sudo -u cucluster ssh ucbu-aricent-vm93 "source /opt/cisco/connection/lib/connection.profile; $INFORMIXDIR/bin/dbaccess sysmaster@ciscounity") for sid in $sessionid_remote;do if * ]];... (2 Replies)
Discussion started by: deeptis
2 Replies

4. Shell Programming and Scripting

[Solved] need to convert decimal to integer

Using below command awk 'NR==FNR{A=$1;next} {sum+=($2*A)}END{OFMT="%20f";print int(sum)}' Market.txt Product.txt answer:351770174.00000 how to convert this to 351770174. when i try with below command i am getting different result. awk 'NR==FNR{A=$1;next}... (3 Replies)
Discussion started by: katakamvivek
3 Replies

5. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

6. Shell Programming and Scripting

Convert to Integer

Hi fellows!! i'm doing something which is not working out for me properly which i don't understand why nowdate=`date +%s` echo $nowdate now the problem how to convert a date which is stored in a variable mydate="22/Oct/2011" mydate=`date -d '$mydate' +%s` it gives error... (11 Replies)
Discussion started by: me_newbie
11 Replies

7. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

8. Shell Programming and Scripting

how to convert string to an integer and how to do calculations like add.,sub.,mult. on it

How to convert string into an integer or number For example : % set tim = `date` % echo $tim Tue Feb 22 16:25:08 IST 2011 here How to increment time by 10 hrs like 16+10 , here 16 is a string in date cmd. .. how to convert 16 to an integer and added to a another nimber ? Thanks... (3 Replies)
Discussion started by: sbhamidi
3 Replies

9. Shell Programming and Scripting

counting non integer number in awk

Hi, I am having the following number in the file tmp 31013.004 20675.336 43318.190 30512.926 48992.559 277893.111 41831.330 8749.113 415980.576 28273.054 I want to add these numbers, I am using following script awk 'END{print s}{s += $1}' tmp its giving answer 947239 which is correct,... (3 Replies)
Discussion started by: chaitubek
3 Replies

10. UNIX for Dummies Questions & Answers

convert from an integer to a string

i want to convert from an integer to a string..in unix...i am writing a C program with embedded SQL... I remeber using itoa...but for some reason it doesnt work......i cant find it in the manual..... Maybe that is the wrong command..... but i have checked Dev Studio.....and it doest exist in the... (6 Replies)
Discussion started by: mojomonkeyhelper
6 Replies
Login or Register to Ask a Question