Output from who command - quick riddle


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output from who command - quick riddle
# 8  
Old 02-06-2011
You forgot to turn spaces into tabs again

Code:
who | grep s132a | cut -d' ' -f1,3,11,13,14 | tr ' ' '\t'

and if that doesn't work, I'll need to see the output to understand why, my crystal ball is still down for scheduled maintenance.
# 9  
Old 02-06-2011
Thank you for your patience with me but I think we are almost there!

When I use this code:

Code:
echo -e "User\tTTY\tDate\tTime"
who | grep s132a | cut -d' ' -f1,3,11,13,14 | tr ' ' '\t'

it is ALMOST perfect except when I cut the fields you will see the date is cut as two fields. One is FEB, the other is 6. What happens is a tab now gets inserted between FEB and 6 and I want these two fields together with only a space. Here is the output that happens:

Code:
User    TTY     Date    Time
s132a14 pts/2   Feb     6       12:27

# 10  
Old 02-06-2011
This is why I wanted to know the output of who. It's much harder to fix this after you process it than to get it right in the first place! But since you just plain won't, this hack might do:
Code:
who | grep s132a | cut -d' ' -f1,3,11,13,14 | while read A B C D
do
        echo -e "${A}\t${B}\t${C}\t${D}"
done

---------- Post updated at 12:25 PM ---------- Previous update was at 12:24 PM ----------

This might be a simpler way:

Code:
who | grep s132a | cut -d' ' -f1,3,11,13,14 --output-delimiter='\t'

which if it works, the parameter --output-delimiter='\t' could replace
Code:
cut ... | tr ' ' '\t'

in most places.

Last edited by Corona688; 02-06-2011 at 02:30 PM..
# 11  
Old 02-06-2011
Sorry. Here is the pure output of the who command. There must be a simple way to make the headers line up over the fields. Trying to figure it out with another book I just went out and bought also.

Code:
[b1245~]$ who
b13323x  pts/1        Feb  6 11:21 (c-6-30-132)
b245645  pts/2        Feb  6 12:36 (c-2-2-143)
b534453  pts/3        Feb  6 12:07 (c-7-24-6)]

When I dont use grep I get it to work fine like this:

Code:
echo -e "User\tTTY\tDate\tTime"
who | cut -c1-7,9-15,23-25,28-34 | tr ' ' '\t'

Code:
echo -e "User\tTTY\tDate\tTime"
who | grep s132a | cut -d' ' -f1,3,11,13,14

But im trying to make it line up with something like this:
# 12  
Old 02-06-2011
Thanks for giving me real data to test with. Now I can get it right with:

Code:
who | while read A B C D E ; do echo -e "${A}\t${B}\t${C} ${D}\t${E}" ; done

# 13  
Old 02-06-2011
What Operating System and version do you have? (There is much variation in "who").
What Shell do you use?

Though I can't think of a "who" which outputs the terminal address in the posted format, nothing surprises me.

In most O/S the "who" command (with no parameters) does not give the address of the calling computer. You normally need "who -u" or "who -R".

Assuming you have a fairly mainstream O/S I would expect "who -s" to list only the fields you require. The command "who -sH" should put standard headings on the columns (but not the ones you require).

Beware that because we are in the early part of the month the day-of-month is a single digit. To keep the headings in consistent alignment you may need an additional space when compared with the example in Post #1 .
# 14  
Old 02-06-2011
Quote:
Originally Posted by methyl
What Operating System and version do you have? (There is much variation in "who").
I managed to drag that out of him earlier. He's using windows 7 with GNU utilities, probably cygwin. shell is bash.
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

4. Shell Programming and Scripting

Ksh riddle: interpret variable two times?

exam is a ksh script. In command line I enter: exam 3 param_2 param_3 param_4. In exam how can I get the value of the parameter which position is specified by the first argument. Simply doing this DOES NOT work: offset=$1 value=$$offset can you figure out any possible way to interpret a... (5 Replies)
Discussion started by: i27oak
5 Replies

5. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

6. Shell Programming and Scripting

Quick Question on sed command in shell script

Hello, I have the following line in one of my shell scripts. It works fine when the search string($SERACH_STR) exists in the logfile($ALERTLOG) but if the search string does not exist this line errors out at run time. Is there a way to make this line return 0 if it is not able to find the... (4 Replies)
Discussion started by: luft
4 Replies

7. Shell Programming and Scripting

Riddle - solve it if you can

I don't understand this, can anyone explain the evaluation logic used here, and I would really appreciate a general explanation for it. ---------------------- Here's the korn script: -------------------- #! /usr/bin/ksh if ] then echo true else echo false fi if (( 2 > 10 )) then... (1 Reply)
Discussion started by: numstr
1 Replies

8. UNIX for Dummies Questions & Answers

Quick help with the date command...

I'm new to unix, and I was wondering how would you output the following date using the date command? Thank you in advance. November 4,2005 17:04:31 EST Wednesday (1 Reply)
Discussion started by: Newguy4
1 Replies
Login or Register to Ask a Question