1

I'm using this command to check all sshd processes and CPU USAGE and CMD for sshd deamon:

ps -C sshd -o %cpu,cmd

example of output:

%CPU CMD
 0.0 sshd: root [priv]
 0.0 sshd: aadmin@pts/1

After that i've used:

ps -C sshd -o %cpu,cmd | awk '!/%CPU/ {print $1, $3}'

Output:

0.0 root
0.0 aadmin@pts/1

To sort CPU USAGE and third line, what i want is to assign awk output var to have output like this for multiple processes and in separate lines.

CPU_USAGE=0.0 USER=root
CPU_USAGE=0.0 USER=aadmin@pts/1

I've tried to assign variable to awk with awk -v but without success

5
  • Is the question how to get your output to look like that? Commented Feb 5, 2015 at 12:18
  • 1
    Maybe I'm missing something here but couldn't you just change your print to print "CPU_USAGE="$1, "USER="$3? Commented Feb 5, 2015 at 12:18
  • @EtanReisner yes, like this CPU_USAGE=0.0 USER=root CPU_USAGE=0.0 USER=aadmin@pts/1 Commented Feb 5, 2015 at 12:20
  • 1
    It's not clear what any of this has to do with variables. Can you update your question to explain what you mean? Commented Feb 5, 2015 at 12:41
  • It's also not clear what any of it has to do with To sort CPU USAGE and third line. Sort what? What third line? Commented Feb 5, 2015 at 13:47

1 Answer 1

3
ps -C sshd -o %cpu,cmd | awk '!/%CPU/ {printf("CPU_USAGE=%s USER=%s\n", $1, $3)}'

You just have to format your print statement, for that, use printf.

Or shorter:

ps -C sshd -o %cpu,cmd | awk '!/%CPU/ {print "CPU_USAGE="$1, "USER="$3}'

Hope this is what u want. Your question was not clear otherwise...

Sign up to request clarification or add additional context in comments.

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.