0

This is what I got so far:

var="1.5"

echo "date,1%sa,comment" |
 awk -F ',' '{print $2}' |
 awk -v i="$var" -F '%' '{$1*=i; print $1}'

# prints: 1.5

I need to get the value to calculate with from a variable outside awk so using -v. In the code I use two awk commands to separate the number from the %sa. How can I combine those 2 commands and reprint the whole line like this:

date,1.5,comment
1
var="1.5"

echo "date,1%sa,comment" |
awk -F ',' -v OFS=',' -v i="$var" '{ split($2, a, "%"); $2 = a[1]*i; print }'

This uses commas for input and output separators and then splits the second field on %, sets it to the number in front of the % times the value of the variable passed into the program, and prints the modified record.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.