Problem printing with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem printing with awk
# 1  
Old 11-26-2017
Problem printing with awk

Hi, so i have this command:

Code:
ps axo cmd=,user=,%cpu=,priority= --sort=-%cpu | awk '{print $2}'

And i want to print the second column, that is user. The thing is that i dont know why this is my output:

Code:
root
Eskizoi+
Eskizoi+
axo
{print

I dont know why axo and {print are there, the three first users are correct but i dont know what im doing wrong. If i change $2 to $1 the result is correct, so i dont know whats failing.

Thank you very much
# 2  
Old 11-26-2017
Some of the ps output writes more columns than the others.

You could try taking the output from the end:

e.g.
Code:
ps ... | awk '{print $(NF-2)}'

Or, for a unique list:
Code:
ps ... | awk '!A[$(NF-2)]++ {print $(NF-2)}'

Or change your ps command to put the user first, then print $1.

Question: If all you want is the username, why add the other columns to ps in the first place? What do you want this information to do with?
# 3  
Old 11-26-2017
My output from what you gave me:

Code:
CMD                         USER
/init                           root
/bin/bash                   Eskizoide
/bin/bash                   Eskizoide
axo
{print

print and axo are still there, and that prints the 2 columns, i only want the second. Thx for the reply.

EDIT: mmm, its gotta be something with how i wrote the command because this works with any column.

Code:
ps axo pid,%cpu | awk '{print $2}'

# 4  
Old 11-26-2017
If you want the user and the command only:
Code:
ps axo user=,cmd= --sort=-%cpu

"print" and "axo" are there because they're part of the command (cmd=) column output by ps. cmd is multi-word, so you can't assume that $2 will contain the username. If you change the order (user=,cmd=) then you can assume that "$1" will contain the username (assuming the username does not contain spaces).

Code:
ps axo user=,cmd= --sort=-%cpu | while read user cmd; do
    echo "User: $user, Command: $cmd"
done

# 5  
Old 11-26-2017
Another approach is to generate a delimited output.
Code:
ps -eo "%c,%U,%C,%n"

Format descriptors used:-
Code:
       CODE   NORMAL   HEADER
       %C     pcpu     %CPU
       %U     user     USER
       %c     comm     COMMAND
       %n     nice     NI

Then use awk to print unique values:-
Code:
ps -eo "%c,%U,%C,%n" | awk -F, '{A[$2]}END{for(k in A) print k}'

# 6  
Old 11-27-2017
Thx for the replies, i was doing something very dumb, i didnt know i could show ionly one column from ps and sort it from cpu usage. like:

Code:
ps axo user= --sort=-%cpu

Im very new to this so i didnt know it. What a i want to do is to create a table with printf with different attributes from a process, then call my script without arguments to print by default all those processes with their attributes, but i also want to call my script with arguments to show me only processes from certain user or only processes with priority between "x" and "z".

I think now i know how to do it so thank you very much.

Ah, one last question, i having this problem with printf where it prints all in one column not with the format a said to it.

This is the command:

Code:
printf "%10s %25s %10s %30s %10s %10s %20s %50s\n" "$(ps axo cmd= --sort=-%cpu)" "$(ps axo user= --sort=-%cpu)"

This the output:

Code:
/init
/bin/bash
ps axo cmd= --sort=-%cpu  root
Eskizoide
Eskizoide

I dont know if im missing something but i think the syntax is correct.
# 7  
Old 11-27-2017
printf's format string doesn't seem to match the data supplied. You may have to rearrange the pss' output first, and then printf it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: couldn't print out stored variable in awk 2. Relevant commands, code, scripts, algorithms: i have in a... (5 Replies)
Discussion started by: ymc1g11
5 Replies

2. Shell Programming and Scripting

Problem with printing

Hi, i am writing a script to generate a summary report for my data text file my data in my text file goes like this: MacOS:Mary Abraham:53.48:88:38 Windows in lala:Mary Abraham:22.30:23:22 lala in Windows:John Goodman:33.60:121:12 Hello world:Mr Tian:23.30:23:10 Windows in 31 days:John... (2 Replies)
Discussion started by: Cheranime
2 Replies

3. UNIX for Dummies Questions & Answers

Problem with formatted printing in AWK

Hi, I want to print 130 fileds using formatted printing in AWK. It looks like awk '{printf ("%7.2f%7.2f...%7.2\n",$1,$2,...,$130)}' inflie>oufile But it gives me an error: Word too long! Can you please help me with this? Is there another way to do this? (1 Reply)
Discussion started by: PHL
1 Replies

4. SCO

Problem with printing

Does anybody know how I can stop the system slewing pages when a spool file has finished printing. I need to set up a method where all users are printing to the same printer to produce labels. The problem is that when the first users file has been printed the pages are slewed and the label is not... (2 Replies)
Discussion started by: rongrout
2 Replies

5. HP-UX

Printing problem

I have scheduled to print a numerous of report in mid-night. But, I found that some reports have not been printed out. Then, I checked the file /var/spool/lp/log and found that all reports have been submitted to print queue. Could anyone tell me what can be done to ensure all print jobs can be... (2 Replies)
Discussion started by: alfredo
2 Replies

6. UNIX for Dummies Questions & Answers

Printing problem

Hello, Not sure where to begin on describing the issue; please try to understand that I am not very familiar with SCO UNIXWARE. I am a newbie... We have a SCO UNIXWARE version 7.4 when we restart the computer we are required to enter the command lpstart, without it; people cannot print. ... (1 Reply)
Discussion started by: Yorgy
1 Replies

7. UNIX for Dummies Questions & Answers

printing Problem

When I print a file from an application it never stops printing. Any ideas? (2 Replies)
Discussion started by: 744eagle
2 Replies

8. IP Networking

Printing Problem

I have a printer installed on sun solaris m/c sun os 5.8. It doesn't print more than 52 files i.e if a give lp *.txt and the curent folder contains more than 52 files then it doesn't print them but if print in batches , each containing 52 files then it prints them properly. How do i increase the... (3 Replies)
Discussion started by: kapilv
3 Replies

9. UNIX for Dummies Questions & Answers

printing problem

i can only print from unix when I am logged in as host. When any user logs in (even myself as a user who has root priviledges) I cannot print. Has anyone heard of this problem before and are there any troubleshooting tips out there. HELP......... (3 Replies)
Discussion started by: acarroll
3 Replies

10. UNIX for Dummies Questions & Answers

Problem with printing

Hi , Can somebody Please help me in finding out where I am going wrong. Suppose i have a file called r_wer which contains files. for ex : LMN20001 LMN20002 LMN20003 Now I want to print them. The way I am doing is $ for a in `cat '/r_wer'` > do > lp -d printername $a > done But... (3 Replies)
Discussion started by: rooh
3 Replies
Login or Register to Ask a Question