You are not logged in.

#1 2024-01-22 02:03:15

andyturfer
Member
Registered: 2021-01-08
Posts: 90

[SOLVED] Trying to run awk with watch

I'm trying to run

awk '{print $1*1e-6 " W"}' /sys/class/power_supply/BAT0/power_now

but with "watch". For example, something like:

watch -n3 -t awk '{print $1*1e-6 " W"}' /sys/class/power_supply/BAT0/power_now

The above doesn't work though. I've tried wrapping the "awk" part in quotes and escaping the inner quotes (etc), but it still doesn't work.

How can I get the above to work (so I can watch the output of awk '{print $1*1e-6 " W"}' /sys/class/power_supply/BAT0/power_now)?

Last edited by andyturfer (2024-01-22 02:15:17)

Offline

#2 2024-01-22 02:12:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [SOLVED] Trying to run awk with watch

You'll need to escape the dollar sign as well.  This works:

watch -n1 -t 'awk "{print \$1*1e-6 \" W\"}" /sys/class/power_supply/BAT0/power_now'

Of course, "watch" has always struck me as silly.  A loop is simpler and cleaner:

while :; do awk '{print $1*1e-6 " W"}' /sys/class/power_supply/BAT0/power_now; sleep 3; done

(you could add a clear screen ansi sequence to the print if you really wanted that visual effect).

EDIT: note, the -x flag to watch would probably also help ... but the existence of that flag makes the whole "watch" program that much more silly (i.e., that "-x" is not what it's doing by default is pretty ridiculous).

Last edited by Trilby (2024-01-22 03:03:21)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2024-01-22 02:15:00

andyturfer
Member
Registered: 2021-01-08
Posts: 90

Re: [SOLVED] Trying to run awk with watch

Nice one - thanks!

Offline

Board footer

Powered by FluxBB