Bash expr with 0 before decimals [duplicate] - bash

This question already has answers here:
How do I get bc(1) to print the leading zero?
(13 answers)
Closed 8 years ago.
I am summing two small decimal numbers contained in two variables:
SEEupper=`expr $SEEmedian+$SEEthre | bc`
but since the result is a number smaller than 1, like 0.XXXX, the output is: '.XXXX'.
Is there any way to have an output with the '0' before the dot and the decimals?

workaround: ... | sed -e "s|^\.|0.|"

if you can use python:
SEEupper=`python -c "print $SEEmedian +$SEEthre"`

Yes, with the internal bash command printf:
printf "%g" $SEEupper

Related

How can I append the same byte to a file a specified number of times? [duplicate]

This question already has answers here:
Bash: repeat character a variable number of times
(3 answers)
Closed 4 months ago.
I have the following code:
yes "$(echo -e "\xff")" | head -n 10 > SomeFile.bin
Which writes 10 times 0xFF and 0x0A (newline) to SomeFile.bin. But my objective is to fill the file with FF.
Is there a way to print only consecutive 0xFF values instead? Without adding a newline every time?
Here is one way:
printf '\xFF%.s' {1..10} >SomeFile.bin

How to extract value between two words in shell [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string like this:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
I want to extract 46942 and put in a variable.
I tried this, but it only removes the Processed and total. How do I do it?
Var=$(echo $line | sed -e 's/Processed\(.*\)total/\1/')
Use parameter expansion:
line="06:13:41.817 INFO ProgressMeter - Traversal complete. Processed 46942 total variants in 2.2 minutes."
line=${line#*'Processed '} # Remove from left up to "Processed ".
line=${line%' total'*} # Remove from from " total" up to the end.

How do I print the "zero" between the minus and the decimal places on Bash shell [duplicate]

This question already has answers here:
How do I get bc(1) to print the leading zero?
(13 answers)
Closed 2 years ago.
With dc external tool bash tend to not print at the right of the decimal point.
#!/bin/bash
a[0]=-0.5
`echo "scale=10;${a[0]}/1"|bc -l`
With the command represented above bash will print -.5000000000.
How can I add the zero between the minus signal and the point -0.5000000000
PS: I do print a[1]=0 with 10 decimal cases?
Instead of bc you may consider this awk that does floating point match and formatting using printf:
a[0]=-0.5
awk -v n="${a[0]}" 'BEGIN{printf "%.10f\n", n/1}'
-0.5000000000

division in shell script [duplicate]

This question already has answers here:
floating point accuracy with bash
(2 answers)
Closed 6 years ago.
I am trying to do a simple division of variables in a shell script, but it is somehow not working. May be there I am overlooking something really basic. Below is a script for calculating total number of reads from a bam file for bams files read from a list file. I need to assign the output to a variable and calculate a ratio. This is what I have:
normNum=100000
while IFS=$'\t' read -r bamfile name; do
#for i in $(ls *.bam)
echo $bamfile
mappedReads="$(samtools idxstats $bamfile | awk '{s+=$3} END {print s}')"
echo $normNum
echo $mappedReads
#scalingFactor="$((normNum / mappedReads))"
#echo $scalingFactor
scalingFactor=`printf "%0.3f\n" $((normNum / mappedReads))`
echo $scalingFactor
done < "${file}_temp"
The different prints are giving my correct number except for scalingFactor, which gives me 0.
merged_DupRem.bam
100000
24226512
0.000
Any pointers please?
Thanks..
Bash does integer math, not floating point. You will need to use awk or bc to provide floating point output. e.g. with bc
scalingFactor=$(printf "scale=3; %d/%d\n" $normNum $mappedReads | bc)

How to evaluate mathematics in Ubuntu console? [duplicate]

This question already has answers here:
Bash: evaluate a mathematical term?
(9 answers)
Closed 6 years ago.
When I try to calculate a maths problem such as 1 + 1 using the Ubuntu console like this:
user#servername:~$ 1 + 1
Ubuntu thinks the first 1 is a command and I get this error:
1: command not found
I then tried to use echo to evaluate the string (with and without quotes) like this:
user#servername:~$ echo 1 + 1
user#servername:~$ echo "1 + 1"
Unfortunatly both output 1 + 1 and not 2.
It would also be greatly appreciated to include a explanation as to why echo does not evaluate the specified string before outputting it?
Also is there is a built-in command that evaluates a string before outputting it (maybe something that behaves like eval in Python)?
Thank you for the help in advance.
The one I usually use is
bc<<<2+2
You can make that easier to type with an alias:
$ alias x='bc<<<'
$ x 53*(27-23)
212
If you put whitespace in the expression you'll need to quote it. Also if the expression starts with an open parenthesis (as opposed to having one in the middle).
$ echo $((1+1))
2
$ echo "1+1" | bc
2
$ awk 'BEGIN{print 1+1}'
2

Resources