I want to evaluate the expression and display the output correct to 3 decimal places i tried with below code but it is not working help me how to do that in bash.
echo -e "Enter expression to calculate : \c"
read num
let a=num
printf '%f\n' "$a"
Input : 5+50*3/20 + (19*2)/7
Output : 17.000000
Desired Output : 17.929
With bc:
echo 'scale=3; 5+50*3/20 + (19*2)/7' | bc -l
Output:
17.928
Instead of let a=num, you could for example:
echo $num | bc
17.92857142857142857142
This is one of the very rare cases where it's acceptable to let a shell variable expand to become part of the body of an awk script:
$ num='5+50*3/20 + (19*2)/7'
$ awk 'BEGIN{print '"$num"'}'
17.9286
$ awk 'BEGIN{printf "%0.3f\n", '"$num"'}'
17.929
Related
I am looking for a simpler, more idiomatic and most importantly standards (POSIX) compliant way to reverse an IP address, using sh the Unix shell.
Here's my current solution:
RevIP() {
echo "$1"|tr . "\n"|tac|tr "\n" .
# 'Split' on the dot character, reverse the list, 'join' with dot
}
Example usage:
$ RevIP 23.45.67.89
89.67.45.23.
Issues:
this uses tac which is not in POSIX
the output ends in . and there's no newline. Not a major problem but ideally it should end in \n instead.
This could be solved by an additional |sed s/... at the end but is there a way to do this more elegantly so there's no need for that?
One doesn't need external commands for this; shell builtins suffice.
reversed=$(echo "$ip" | { IFS=. read q1 q2 q3 q4; echo "$q4.$q3.$q2.$q1"; })
...or, a little more efficiently (but a little less tersely):
IFS=. read q1 q2 q3 q4 <<EOF
$ip
EOF
reversed="$q4.$q3.$q2.$q1"
this should be one possibility for what you're looking
#!/bin/sh
ip="$1"
reversed=$(echo "$ip" | awk -F '.' '{print $4 "." $3 "." $2 "." $1}')
echo "$reversed"
$ echo '23.45.67.89' |
> mawk '$!NF = sprintf((_ = "%s.%s") "." _,
> $(_ = NF), $--_, $--_, $--_)' FS='[.]'
89.67.45.23
i am passing a argument and that argument i have to match in file and extract the information. Could you please how I can get it?
Example:
I have below details in file-
iMedical_Refined_load_Procs_task_id=970113
HV_Rawlayer_Execution_Process=988835
iMedical_HV_Refined_Load=988836
DHS_RawLayer_Execution_Process=988833
iMedical_DHS_Refined_Load=988834
If I am passing 'hv' as argument so it should to pick 'iMedical_HV_Refined_Load' and give the result - '988836'
If I am passing 'dhs' so it should pick - 'iMedical_DHS_Refined_Load' and give the result = '988834'
I tried below logic but its not giving the result correctly. What Changes I need to do-
echo $1 | tr a-z A-Z
g=${1^^}
echo $g
echo $1
val=$(awk -F= -v s="$g" '$g ~ s{print $2}' /medaff/Scripts/Aggrify/sltconfig.cfg)
echo "TASK ID is $val"
Assuming your matching criteria is the first string after delimiter _ and the output needed is the numbers after the = char, then you can try this sed
$ sed -n "/_$1/I{s/[^=]*=\(.*\)/\1/p}" input_file
$ read -r input
hv
$ sed -n "/_$input/I{s/[^=]*=\(.*\)/\1/p}" input_file
988836
$ read -r input
dhs
$ sed -n "/_$input/I{s/[^=]*=\(.*\)/\1/p}" input_file
988834
If I'm reading it right, 2 quick versions -
$: cat 1
awk -F= -v s="_${1^^}_" '$1~s{print $2}' file
$: cat 2
sed -En "/_${1^^}_/{s/^.*=//;p;}" file
Both basically the same logic.
In pure bash -
$: cat 3
while IFS='=' read key val; do [[ "$key" =~ "_${1^^}_" ]] && echo "$val"; done < file
That's a lot less efficient, though.
If you know for sure there will be only one hit, all these could be improved a bit by short-circuit exits, but on such a small sample it won't matter at all. If you have a larger dataset to read, then I strongly suggest you formalize your specs better than "in this set I should get...".
I have string like
name1::1.1.1.1::ps -ax
I want to split the string based on delimiter :: using bash scripting.
The desired output should be an array of 3 elements
("name1" "1.1.1.1" "ps -ax")
without double quotes
I appreciate your help.
Assuming there are no :s in the array data, use bash pattern substitution to squeeze the :: to : while assigning the string to $array, then show the whole array, then just element #2:
a="name1::1.1.1.1::ps -ax"
IFS=: array=(${a//::/:}) ; echo ${array[#]} ; echo "${array[2]}"
Output:
name1 1.1.1.1 ps -ax
ps -ax
But what if there are :s in the array data? Specifically in the third field, (the command), and only in that field. Use read with dummy variables to absorb the extra :: separators:
a="name1::1.1.1.1::parallel echo ::: 1 2 3 ::: a b"
IFS=: read x a y b z <<< "$a"; array=("$x" "$y" "$z"); printf "%s\n" "${array[#]}"
Output:
name1
1.1.1.1
parallel echo ::: 1 2 3 ::: a b
The only safe possibility is use a loop:
a='name1::1.1.1.1::ps -ax'
array=()
a+=:: # artificially append the separator
while [[ $a ]]; do
array+=( "${a%%::*}" )
a=${a#*::}
done
This will work with any symbol in a (spaces, glob characters, newlines, etc.)
echo "name1::1.1.1.1::ps -ax" | awk -F"::" '{print $1 $2 $3}'
i=0
string="name1::1.1.1.1::ps -ax"
echo "$string" | awk 'BEGIN{FS="::";OFS="\n"}{$1=$1;print $0}'>tempFile
while read line;
do
arr["$i"]="$line"
i=$(expr $i + 1)
done<tempFile
echo "${arr[#]}"
echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"
Output:
sh-4.4$ ./script1.sh
name1 1.1.1.1 ps -ax
name1
1.1.1.1
ps -ax
I have a version number with three columns and two digits (xx:xx:xx). Can anyone please tell me how to increment that using shell script.
Min Value
00:00:00
Max Value
99:99:99
Sample IO
10:23:56 -> 10:23:57
62:54:99 -> 62:55:00
87:99:99 -> 88:00:00
As a one liner using awk, assuming VERSION is a variable with the version in it:
echo $VERSION | awk 'BEGIN { FS=":" } { $3++; if ($3 > 99) { $3=0; $2++; if ($2 > 99) { $2=0; $1++ } } } { printf "%02d:%02d:%02d\n", $1, $2, $3 }'
Nothing fancy (other than Bash) needed:
$ ver=87:99:99
$ echo "$ver"
87:99:99
$ printf -v ver '%06d' $((10#${ver//:}+1))
$ ver=${ver%????}:${ver: -4:2}:${ver: -2:2}
$ echo "$ver"
88:00:00
We just use the parameter expansion ${ver//:} to remove the colons: we're then left with a usual decimal number, increment it and reformat it using printf; then use some more parameter expansions to group the digits.
This assumes that ver has already been thorougly checked (with a regex or glob).
It's easy, just needs some little math tricks and bc command, here is how:
#!/bin/bash
# read VERSION from $1 into VER
IFS=':' read -r -a VER <<< "$1"
# increment by 1
INCR=$(echo "ibase=10; ${VER[0]}*100*100+${VER[1]}*100+${VER[2]}+1"|bc)
# prepend zeros
INCR=$(printf "%06d" ${INCR})
# output the result
echo ${INCR:0:2}:${INCR:2:2}:${INCR:4:2}
If you need overflow checking you can do it with the trick like INCR statement.
This basically works, but may or may not do string padding:
IN=43:99:99
F1=`echo $IN | cut -f1 '-d:'`
F2=`echo $IN | cut -f2 '-d:'`
F3=`echo $IN | cut -f3 '-d:'`
F3=$(( F3 + 1 ))
if [ "$F3" -gt 99 ] ; then F3=00 ; F2=$(( F2 + 1 )) ; fi
if [ "$F2" -gt 99 ] ; then F2=00 ; F1=$(( F1 + 1 )) ; fi
OUT="$F1:$F2:$F3"
echo $OUT
try this one liner:
awk '{gsub(/:/,"");$0++;gsub(/../,"&:");sub(/:$/,"")}7'
tests:
kent$ awk '{gsub(/:/,"");$0++;gsub(/../,"&:");sub(/:$/,"")}7' <<< "22:33:99"
22:34:00
kent$ awk '{gsub(/:/,"");$0++;gsub(/../,"&:");sub(/:$/,"")}7' <<< "22:99:99"
23:00:00
kent$ awk '{gsub(/:/,"");$0++;gsub(/../,"&:");sub(/:$/,"")}7' <<< "22:99:88"
22:99:89
Note, corner cases were not tested.
Just a stupid question. Provide a code snippet
b="a=2"
How to extract the value 2 and assign it to variable a
You could just eval the code..
eval $b
I am renowned to give ugly solutions so I won't dissapoint you -
[jaypal:~/Temp] a=$(awk '{print $(NF-1)}' FS='["=]' <<< 'b="a=2"')
[jaypal:~/Temp] echo $a
2
Less intense solution
[jaypal:~/Temp] a=$(awk -F= '{print $NF}' <<< $b)
[jaypal:~/Temp] echo $a
2
a=${b#a=}
Take the value of $b, remove the leading text a=; assign what's left (2) to a.
$ b="a=2"
$ var=${b##*=}; echo $var
2
If you are looking for a shell utility to do something like that, you can use the cut command.
To take your example, try:
echo "abcdefg" | cut -c3-5
Where -cN-M tells the cut command to return columns N to M, inclusive.
REF:
What linux shell command returns a part of a string?
and
Extract substring in Bash
another portable solution
IFS='='
set -- $b
let "$1=$2"