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 ...
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)).
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#$(...
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♦
- 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♦
- 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 (...
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}"...
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, ...
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
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]) };
...
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
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 ...
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}
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 { ...
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 ...
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 ...
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 ...
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 '$_=...
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 ...
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
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, -...
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 ...
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....
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 ...
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 ...
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....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
numeric-data × 243text-processing × 89
awk × 84
shell-script × 37
bash × 27
linux × 26
sed × 25
grep × 20
sort × 20
shell × 13
command-line × 12
scripting × 12
regular-expression × 11
text-formatting × 8
test × 8
arithmetic × 8
printf × 7
floating-point × 7
columns × 6
bc × 6
variable × 5
csv × 5
random × 5
perl × 4
file-comparison × 4