1

I'm trying to generate a file to keep the log of average CPU usage.

sudo mpstat 1 5 | grep Average | sed -E 's|Average:|$(date)|g' > cpu_usage_test.txt

I'm trying to replace the text "Average:" with the current date and time. Tried different ways but in output, it is displaying $(date) as a string. To test it easily, use the following command to display it on the terminal itself.

sudo mpstat 1 1 | grep Average | sed -E 's|Average:|$(date)|g'
2

The shell does not substitute anything in a string enclosed in single quotes. Use double quotes instead.

You don't need the -E option, and you can do without grep and without sudo:

mpstat 1 1 | sed -n "s|Average:|$(date)|gp"

-E is not needed because you don't use extended regular expressions here. -n prevents sed from printing by default, and the p at the end prints the line only when "Average:" is in fact replaced.

The GNU sed manual is not bad for learning, by the way.

1

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.