1

This is the code I've written, it works:

awk "BEGIN{print int(($high-32)*5/9)  int(($low-32)*5/9)}";

Results in: 1911

I would like to the '19' and '11' seperated by " | " so that the output looks like this: 19 | 11

I have tried to insert " \| " both as a literal and a var but both resulted in errors. Haven't seen much in the various forums. What am I missing? Thanks.

Irvin
  • 23
  • 2

2 Answers2

2

Assuming you're on a Unix system you should be using single quotes rather than double around your script (see https://mywiki.wooledge.org/Quotes), and using awk variables rather than allowing shell variables to expand to become part of the body of your awk script (see How do I use shell variables in an awk script?), e.g.:

awk -v high="$high" -v low="$low" 'BEGIN{print int((high-32)*5/9)  int((low-32)*5/9)}'

and then the enhancement you're asking for would be:

awk -v high="$high" -v low="$low" -v OFS=' | ' 'BEGIN{print int((high-32)*5/9), int((low-32)*5/9)}'

or if you prefer:

awk -v high="$high" -v low="$low" 'BEGIN{print int((high-32)*5/9) " | " int((low-32)*5/9)}'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • I should have mentioned I'm running on an older Mac running Mavericks 10.9. I Using your second example I was able to make it work with the code: .... awk -v OFS="|" "BEGIN{print int(($high-32)*5/9), int(($low-32)*5/9)}"; .... Thanks for the help! – Irvin Jun 14 '23 at 12:34
  • 1
    You're welcome but please re-read my answer - do NOT use double quotes around your script and do NOT let shell variables expand within it. Those are 2 well known anti-patterns that will bite you some day if not today (actually the double quotes already bit you, hence your question). Just do what I show in my answer (as explained in the referenced articles I linked) for a simple, robust, easily maintained/enhanced solution. – Ed Morton Jun 14 '23 at 12:35
  • Single quotes won't work on my MacOS, but the doubles did. – Irvin Jun 14 '23 at 13:31
  • 1
    In what way do they "not work" when you run **exactly** the command shown in my answer - error message, wrong output, no output, something else? Aren't you running bash or some other Unix shell? – Ed Morton Jun 14 '23 at 13:37
  • 1
    My apologies. I originally edited my script and it broke with the single quotes. Not sure why. I went back and inserted all three of your scripts and all three work. – Irvin Jun 15 '23 at 01:14
  • No problem. My best guess would be that you didn't remove the `$`s from in front of `high` and `low` in the awk script, or maybe used single quotes around their `-v` assignments. Anyway, glad it's working for you now. – Ed Morton Jun 15 '23 at 12:25
1

Ed's awk solution works great.

An alternative is to use printf to format the string you want and bc to do the math:

#!/bin/bash

high=67
low=52

printf "%.0f | %.0f\n" $(echo "($high-32)*5/9" | bc) $(echo "($low-32)*5/9" | bc)

Or you can use awk's version of printf:

awk -v high="$high" -v low="$low" 'BEGIN{printf "%s | %s\n",int((high-32)*5/9),int((low-32)*5/9)}'

Either prints:

19 | 11
dawg
  • 98,345
  • 23
  • 131
  • 206