Questions tagged [numeric-data]

238 questions
72
votes
5 answers

How to convert floating point number to integer?

Is this the right way to do float to integer conversion in bash? Is there any other method? flotToint() { printf "%.0f\n" "$@" }
Rahul Patil
  • 24,281
  • 25
  • 80
  • 96
70
votes
12 answers

Is there a unix command that gives the minimum/maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, simple and (I think) common use case. My script which…
Minix
  • 5,735
  • 8
  • 28
  • 45
32
votes
9 answers

How to compare a program's version in a shell script?

Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 The output was 4.8.5 So, I wrote a simple if…
Abhimanyu Saharan
  • 853
  • 4
  • 15
  • 24
25
votes
2 answers

Bash - Integer expression expected

I am making the check for update script for my theme I have 2 text files. First one is called "current.txt" and contains the current version. There is 4.1.1 string in that text file. Second one is called "latest.txt" and contains the latest version.…
superquanganh
  • 3,721
  • 5
  • 14
  • 14
16
votes
1 answer

How do I stop Bash from interpreting octal code instead of integer?

I'm writing a a Horoscope RSS script as an intro to Unix/Linux project, and ran into an issue my Professor couldn't fix in the time I was able to stay after class. Since I couldn't figure out how to use actual dates as what to check, I thought I'd…
Irre Levant
  • 161
  • 1
  • 3
16
votes
10 answers

How can I quickly sum all numbers in a file?

Each line contains text and numbers in one column. I need to calculate the sum of the numbers in each row. How can I do that? Thx example.log contains: time=31sec time=192sec time=18sec time=543sec The answer should be 784
Jack
  • 161
  • 1
  • 1
  • 3
15
votes
2 answers

comm: file is not in sorted order

I used comm to compare two sorted files. Each line in these files are positive integer numbers. But the results show comm: file 1 is not in sorted order comm: file 2 is not in sorted order How come the error even if these two files are sorted?
wenzi
  • 403
  • 2
  • 6
  • 9
14
votes
13 answers

How can I numerically sort a single line of delimited items?

I have a line (or many lines) of numbers that are delimited by an arbitrary character. What UNIX tools can I use to sort each line's items numerically, retaining the delimiter? Examples include: list of numbers; input: 10 50 23 42; sorted: 10 23 42…
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
13
votes
4 answers

How to increment the value of a (decimal) variable (with leading zero) by +1?

I have a file in the name of Build.number with the content value 012 which I need to increment by +1. So, I tried this BN=$($cat Build.number) BN=$(($BN+1)) echo $BN >Build.number but here I am getting the value 11 when I am expecting 013. Can…
Naik
  • 303
  • 3
  • 6
12
votes
3 answers

Bash expansion hexadecimal

I would like to know if there is a way of using bash expansion to view all possibilities of combination for a number of digits in hexadecimal. I can expand in binaries In base 2: echo {0..1}{0..1}{0..1} Which gives back: 000 001 010 011 100 101 110…
David Borges
  • 155
  • 1
  • 9
11
votes
13 answers

How to collapse consecutive numbers into ranges?

Given a sorted input file (or command output) that contains unique numbers, one per line, I would like to collapse all runs of consecutive numbers into ranges such that n n+1 ... n+m becomes n,n+m input…
don_crissti
  • 79,330
  • 30
  • 216
  • 245
11
votes
7 answers

Grep lines starting with 1, but not 10, 11, 100 etc

I have a file of genomic data with tag counts, I want to know how many are represented once: $ grep "^1" file |wc -l includes all lines beginning with 1, so it includes tags represented 10 times, 11, times, 100 times, 1245 times, etc. How do I do…
mah
  • 429
  • 2
  • 4
  • 8
11
votes
6 answers

Bash: How to generate random float number using $RANDOM

Is it possible to generate real random numbers with a specific precision and in a specific range using the Integer Random Generator $RANDOM? For example how we can generate real number with 4 precision between 0 and…
sci9
  • 517
  • 2
  • 7
  • 19
11
votes
6 answers

Reverse a hexadecimal number in bash

Is there a simple command to reverse an hexadecimal number? For example, given the hexadecimal number: 030201 The output should be: 010203 Using the rev command, I get the following: 102030 Update $ bash --version | head -n1 GNU bash, version…
Iñaki Murillo
  • 745
  • 1
  • 4
  • 14
10
votes
1 answer

How to sort lines by float number

I've such a file: name: xxx --- time: 5.4 seconds name: yyy --- time: 3.2 seconds name: zzz --- time: 6.4 seconds ... Now I want to sort this file by these float numbers to generate a new file as below: name: yyy --- time: 3.2 seconds name: xxx ---…
Yves
  • 3,161
  • 7
  • 26
  • 54
1
2 3
15 16