I am writing a linux bash script that reads off the file, \"file.txt\" which has
ID: 641751 • Letter: I
Question
I am writing a linux bash script that reads off the file, "file.txt" which has a series of tokens with letters and numbers. I am having a couple problems with it
my first problem - here I am searching the file for a token with "BPN" followed by three numbers. In this problem there are two occurences of this. One is "BPN070" and the other is "BPN130". I need to display just the number portion and multiply the number by 100.
if
bpn=$(egrep -o 'BPN[0-9][0-9][0-9]' file.txt | cut -c5-6); then
bpn=$(($bpn*100))
printf "Occurence(s): %d" $bpn
fi
my problem is that I get this error :
./file2.st: line 61: 70
30*100: syntax error in expression (error token is "30*100")
My second problem is that I am searching a token beginning with "Z" followed by 4 digits, there is one occurence of this, "Z2002"
I then need to display only the number and turn it into a decimal, "20.02"
z1=$(egrep -o 'Z[0-9][0-9][0-9][0-9]' file.txt | cut -c2-5)
z1=$(($z1/100))
printf "The number is: %d feet " $z1
but it displays as "20"
Explanation / Answer
For the first problem try $(( $bpn * 100 )) or pipe it to "bc" like $(( $bpn*100 | bc))
Edited one:
printf '%.2f' $( echo $z1/100 | bc -l ) It prints up to 2 decimals
If u dont want it replace it with %f instead of %.2f
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.