LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-26-2015, 01:23 PM   #1
T-Dub116
Member
 
Registered: Aug 2013
Location: Dolyestown
Posts: 88

Rep: Reputation: Disabled
w command certain columns


I am looking to display the w (who) command to show only the

User TTY IDLE


http://screencast.com/t/vXb74kguJ

13:15:38 up 8 days, 16:48, 47 users, load average: 1.47, 0.95, 0.51
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
rf3 pts/0 192.168.11.72 05:58 48.00s 6.60s 0.00s -bash
user21 pts/1 192.168.21.177 04:32 8:41m 0.05s 0.00s -bash
user7 pts/2 192.168.11.114 17Mar15 6days 1.07s 0.00s -bash
user7 pts/3 192.168.11.114 20Mar15 4:54m 9.75s 0.00s -bash
user6 pts/4 192.168.11.85 11:02 7.00s 55.58s 0.01s -bash
user14 pts/5 192.168.11.183 05:06 29:08 2.80s 0.01s -bash
user14 pts/6 192.168.11.155 05:30 13:28 2.75s 0.00s -bash
user23 pts/7 192.168.11.62 05:48 25:09 0.95s 0.00s -bash
rf5 pts/8 192.168.11.118 05:58 6:51 5.30s 0.01s -bash
user2 pts/9 192.168.11.140 08:34 1:19 44.74s 0.00s -bash
user14 pts/10 192.168.11.141 06:02 12:14 9.46s 0.00s -bash
rf12 pts/12 192.168.11.69 06:02 2:07 9.44s 0.00s -bash
root pts/13 localhost.locald 12:29 9:10 0.05s 0.05s -bash
rf6 pts/14 192.168.11.79 06:04 14.00s 9.05s 0.00s -bash
user14 pts/15 192.168.21.113 06:23 11:29 1.71s 0.00s -bash
user5 pts/16 192.168.11.126 06:24 6:56 4.59s 0.00s -bash
user5 pts/17 192.168.11.126 06:25 6:19 31.59s 0.01s -bash
user11 pts/18 192.168.11.125 06:26 28:10 2.90s 0.01s -bash
user12 pts/19 192.168.21.123 06:35 13:16 0.88s 0.00s -bash
rf7 pts/20 192.168.11.80 06:35 2:06 7.64s 0.00s -bash
user12 pts/21 192.168.21.123 06:36 41:55 0.12s 0.00s -bash



So far I have this

w | awk {'printf ("6s\ts\n", $1, $2, $3, $5)'} |more
13:20:16 up
USER TTY
rf3 pts/0
user21 pts/1
user7 pts/2
user7 pts/3
user6 pts/4
user14 pts/5
user14 pts/6
user23 pts/7
rf5 pts/8
user2 pts/9
user14 pts/10
rf12 pts/12
root pts/13
This only shows the 2 columns



w |awk '{print $1, $2, $3, $5}'

13:21:01 up 8 16:53,
USER TTY FROM IDLE
rf3 pts/0 192.168.11.72 20.00s
user21 pts/1 192.168.21.177 8:46m
user7 pts/2 192.168.11.114 6days
user7 pts/3 192.168.11.114 4:59m
user6 pts/4 192.168.11.85 3:42
user14 pts/5 192.168.11.183 34:31
user14 pts/6 192.168.11.155 18:51
user23 pts/7 192.168.11.62 30:32
rf5 pts/8 192.168.11.118 12:14
user2 pts/9 192.168.11.140 1.00s
user14 pts/10 192.168.11.141 17:37
rf12 pts/12 192.168.11.69 30.00s
This shows some what hard to read and the columns don't line up correctly


is there a way to tab them in to columns that line up properly, or even something similar to the TOP command where the list will keep refreshing?

Last edited by T-Dub116; 03-26-2015 at 01:32 PM.
 
Old 03-26-2015, 01:33 PM   #2
nelz
LQ Newbie
 
Registered: Aug 2004
Posts: 24

Rep: Reputation: 4
You only have two markers in your printf format string, so it only shows the first two values. Try

w -f | awk {'printf ("6s\t%s\t%s\t%s\n", $1, $2, $3, $5)'}
 
1 members found this post helpful.
Old 03-26-2015, 04:50 PM   #3
T-Dub116
Member
 
Registered: Aug 2013
Location: Dolyestown
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by nelz View Post
You only have two markers in your printf format string, so it only shows the first two values. Try

w -f | awk {'printf ("6s\ts\ts\t%s\n", $1, $2, $3, $5)'}


Thank You Very much. Now I am wondering if I want the from column how can I make the IDLE move over to the right to line up with its column

16:48:46 up 8 20:21,
USER TTY FROM IDLE
rf5 pts/0 192.168.11.118 1:32
system pts/1 192.168.21.167 1:59m
user7 pts/2 192.168.11.114 6days
user7 pts/3 192.168.11.114 1:05m

Sorry the copy and paste does not work well, please look at screen shot.
http://screencast.com/t/rBjAinYI


w | awk {'printf ("\t%s\t%s\t\t%s\t\t%s\n",$1, $2, $3, $5)'} |more
.
.
.

Last edited by T-Dub116; 03-26-2015 at 04:53 PM.
 
Old 03-27-2015, 09:44 PM   #4
Gerard.M.Frey
LQ Newbie
 
Registered: Mar 2015
Posts: 12

Rep: Reputation: Disabled
Quote:
Originally Posted by T-Dub116 View Post
Thank You Very much. Now I am wondering if I want the from column how can I make the IDLE move over to the right to line up with its column
Code:
w -f | awk '{ printf "-10s -5s -15s -8s\n", $1, $2, $3, $5 }'
It will look like this
Code:
21:17:13   up    8:29,           users,  
USER        TTY   FROM            IDLE    
gerardf     :0    -               ?xdm?   
gerardf     pts/0 -               8:15m   
gerardf     pts/1 -               1.00s
** Notice all columns are aligned including IDLE

if you want the first line to be omitted i.e 21:17:13 up 8:29, users, pipe the output to tail

Code:
w -f | awk '{ printf "-10s -5s %-15s %-8s\n", $1, $2, $3, $5 }' | tail -n +2
Here is the new output
Code:
 
USER         TTY   FROM            IDLE    
gerardf      :0    -               ?xdm?   
gerardf      pts/0 -               8:22m   
gerardf      pts/1 -               1.00s
That's it. Hope this helps...

Last edited by Gerard.M.Frey; 03-28-2015 at 08:13 PM.
 
1 members found this post helpful.
Old 03-29-2015, 12:33 AM   #5
Gerard.M.Frey
LQ Newbie
 
Registered: Mar 2015
Posts: 12

Rep: Reputation: Disabled
I was experimenting with my awk syntax and here is a way to add your own headers. Here is my code after a few trial and errors.

Code:
w -f | awk 'BEGIN { format="%-10s %-5s %-15s %-8s\n"; printf format, "Cool User", "TTY", "Location", "IDLE"; printf format, "----------", "-----", "-------------", "-------" } NR==3,NR==$NR { printf "%-10s %-5s %-15s %-8s\n", $1, $2, $3, $5 }'
Here is the output

Code:
Cool User    TTY   Location        IDLE    
----------   ----- -------------   ------- 
gerardf      :0    -               ?xdm?   
gerardf      pts/0 -               4:35m   
gerardf      pts/1 -               7.00s   
gerardf      pts/2 -               46:36
The headers with the dashes makes it more appealing.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run soft command on multiple columns mschlict Linux - Newbie 1 10-12-2014 01:00 PM
[SOLVED] How to specify columns by an array variable in awk command massy Linux - General 7 01-08-2014 08:47 AM
[SOLVED] ps command and its columns Hi_This_is_Dev Linux - Server 2 08-30-2010 10:50 AM
Columns after ps -efH command rjcrews Linux - General 2 03-21-2006 10:45 PM
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 11:52 AM


All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration