0

I have a bash script that contains the following text:

LOAD=`/usr/bin/w |grep "load average"`
echo $LOAD 

when I execute the script it searches for load average in the files inthe directory that is given. After it's executed the output is the following:

10:06:40 up 7 days, 17:21, 3 users, load average: 0.08, 0.06, 0.09

What I want is the $LOAD variable to give the following output:

10:06:40 up 7 days, 17:21, 3 users, load averages: 0.08, 0.06, 0.09

I can't recompile the files in the directory that is given so that is not an option. Any ideas how I am able to achieve this output?

Thanks in advance

9
  • Interesting. What OS are you using? It already says 'load averages' on OSX. Commented Mar 25, 2014 at 9:18
  • 1
    You think it's a good idea to have your script change the output of a system command in a subtle way? Commented Mar 25, 2014 at 9:18
  • @Darragh Interesting, load average with w from procps 3.2.8 on Ubuntu. Same on Gentoo from procps-ng 3.3.2. Commented Mar 25, 2014 at 9:20
  • 1
    @AdrianFrühwirth: Still, I'd rather go with whatever the system generates than modifying it in such ways. Imagine what happens when someone has written a follow-up script that does grep "load average:" because that's what worked with w before... but not with OP's script because he tinkered with the output. Ta-daaa... Commented Mar 25, 2014 at 9:31
  • 1
    here is the source code for Apple's w, it does print averages. This is stone-old BSD code, for what it's worth. Commented Mar 25, 2014 at 9:33

2 Answers 2

4

So you just want to change "average" to "averages"?

echo "$LOAD" | sed 's/average/averages/'
Sign up to request clarification or add additional context in comments.

Comments

1

See parameter expansion. This replaces the first occurance of average with averages:

echo "${LOAD/average/averages}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.