Skip to main content
56 votes
Accepted

Why is Gnome fractional scaling 1.7518248558044434 instead of 1.75?

The preset scale factors (100%, 125%, etc.) get adjusted to the closest values that give a whole number of pre-scaling virtual pixels both horizontally and vertically for your resolution; judging by ...
rakslice's user avatar
  • 1,217
39 votes

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

You can force conversion of a number to a specific base in bash like so: $ foo=400 $ echo $((8#$foo)) 256 $ bar=0100 $ echo $((10#$bar)) 100 In general the format is $((base#value)).
Ignacio Vazquez-Abrams's user avatar
33 votes
Accepted

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

The leading 0 causes Bash to interpret the value as an octal value; 012 octal is 10 decimal, so you get 11. To force the use of decimal, add 10# (as long as the number has no leading sign): BN=10#$(...
Stephen Kitt's user avatar
21 votes
Accepted

How to sort lines by float number

If using GNU sort or compatible, you can use its -g switch to do a general numeric sort: $ sort -g -k5,5 file name: yyy --- time: 3.2 seconds name: xxx --- time: 5.4 seconds name: zzz --- time: 6.4 ...
slm's user avatar
  • 380k
19 votes

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

Sounds like you just want this: $ grep '^1\b' a 1 TGCAG..... 1 TGCAG...... For the counting portion of this: $ grep -c '^1\b' file 2
slm's user avatar
  • 380k
18 votes

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

bash treats constants that start with 0 as octal numbers in its arithmetic expressions, so 011 is actually 9. That's actually a POSIX requirement. Some other shells like mksh or zsh ignore it (...
Stéphane Chazelas's user avatar
17 votes

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

Shorter version, assuming GNU sort: version_greater_equal() { printf '%s\n%s\n' "$2" "$1" | sort --check=quiet --version-sort } version_greater_equal "${gcc_version}"...
MarcH's user avatar
  • 291
17 votes

How to round to 2 decimals in bash like MS Excel does?

printf '%.2f\n' 8.125 Will convert that 8.125 textual representation of a decimal number to an internal binary representation using something like the standard strtod() or strtold() C functions, ...
Stéphane Chazelas's user avatar
16 votes
Accepted

bash + how to calculate percentage from number

bash cannot do floating point math, but you can fake it for things like this if you don't need a lot of precision: $ number=248 $ echo $(( number*80/100 )) 198
DopeGhoti's user avatar
  • 79.2k
16 votes
Accepted

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

With gawk (GNU awk) for the asort() function: gawk -v SEP='*' '{ i=0; split($0, arr, SEP); len=asort(arr); while ( ++i<=len ){ printf("%s%s", i>1?SEP:"", arr[i]) }; ...
αғsнιη's user avatar
  • 41.9k
16 votes
Accepted

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

With awk: awk '$1 == "1" { print; x++ } END { print x, "total matches" }' inputfile
DopeGhoti's user avatar
  • 79.2k
15 votes
Accepted

Convert a list of decimal values in a text file into hex format

You can do this using printf and bash: printf '%08x\n' $(< test.txt) Or using printf and bc...just...because? printf '%08s\n' $(bc <<<"obase=16; $(< test.txt)") In order to print the ...
jesse_b's user avatar
  • 41.5k
15 votes

How to sort each 20 lines in a 1000 line file and save only the sorted line with highest value in each interval to another file?

Via awk: NR%20==1 {max=$4 ; line=$0} { if ($4>max) {max=$4;line=$0} } NR%20==0 {print line}
FelixJN's user avatar
  • 14.1k
14 votes

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

Either of these will pick out lines with a 1 in the first column awk '$1 == 1' grep -w '^1' These can both can be extended so you don't even need the wc to count the lines awk '$1==1 { x++ } END { ...
Chris Davies's user avatar
14 votes

How do I add numbers from two txt files with Bash?

Along the paste lines, but doing the math with bc: $ paste -d+ file1 file2 | bc 7 9 11 13 15 The intermediate result (before bc): $ paste -d+ file1 file2 1+6 2+7 3+8 4+9 5+10 For a more bash-centric ...
Jeff Schaller's user avatar
  • 68.8k
14 votes
Accepted

How to grep and cut numbers from a file and sum them

You can take help from paste to serialize the numbers in a format suitable for bc to do the addition: % grep "30201" logfile.txt | cut -f6 -d "|" 650 1389 945 % grep "30201" logfile.txt | cut -f6 -d ...
heemayl's user avatar
  • 58.1k
14 votes
Accepted

Trim trailing zeroes off a number extracted by jq

Pass the price through tonumber: curl -sS 'https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT' | jq -r '.price | tonumber' This would convert the price from a string to a number using the jq ...
Kusalananda's user avatar
  • 356k
13 votes

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

Using perl there's an obvious version; split the data, sort it, join it back up again. The delimiter needs to be listed twice (once in the split and once in the join) eg for a , perl -lpi -e '$_=...
Stephen Harris's user avatar
13 votes
Accepted

How to round to 2 decimals in bash like MS Excel does?

If your system uses GNU coreutils then the numfmt command should be available, and allows you to choose between rounding ‘up’, ‘down’, ‘from-zero’ (the default), ‘towards-zero’, or ‘nearest’. For ...
steeldriver's user avatar
  • 83.8k
12 votes
Accepted

How do I add numbers from two txt files with Bash?

This is basic task many tools can solve; paste + awk combo seems exceptionally handy: $ paste file1 file2 | awk '{$0=$1+$2}1' 7 9 11 13 15
jimmij's user avatar
  • 48.7k
12 votes
Accepted

How to generate a comma separated list of random ints

You can use paste -s to join lines: shuf -i1-10 | paste -sd, - This uses -i option of shuf to specify a range of positive integers. The output of seq can be piped to shuf: seq 10 | shuf | paste -sd, -...
rowboat's user avatar
  • 3,131
11 votes
Accepted

Bash: How to generate random float number using $RANDOM

awk -v n=10 -v seed="$RANDOM" 'BEGIN { srand(seed); for (i=0; i<n; ++i) printf("%.4f\n", rand()) }' This will output n random numbers (ten in the example) in the range [0,1) with four decimal ...
Kusalananda's user avatar
  • 356k
11 votes

how to keep n decimal places?

With awk: awk '{ printf("%.3g %.3g\n", $1, $2) }' file With the given data, this produces 0.993 0.00704 0.646 0.354 0.993 0.00704 0.993 0.00704 0.993 0.00704 0.993 0.00704 0.993 0.00704 0.993 0....
Kusalananda's user avatar
  • 356k
11 votes

count how many times each number occurs

This awk script prints output as in your example: awk '{ for ( i=1; i<=NF; i++ ) # loop over all fields/columns dict[$i]++; # count occurrence in an array using the ...
Bodo's user avatar
  • 6,441
11 votes
Accepted

"sort -g" does not work as expected on data in scientific notation

2.3e-12 would be understood as 2 in a locale where the decimal radix character is , (as it is in most of the non-English speaking world including your de_DE.utf8) where the number would need to be ...
Stéphane Chazelas's user avatar
10 votes

Inaccurate multiplication in mit-scheme

Remember that computers are binary, No matter how many base 2 digits you’re willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base2 1/10 is 0....
gdahlm's user avatar
  • 1,271

Only top scored, non community-wiki answers of a minimum length are eligible