Output from who command - quick riddle


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output from who command - quick riddle
# 1  
Old 02-06-2011
Output from who command - quick riddle

Hi all.

So I have a problem. I have been doing real good figuring this stuff out on my own but Im a newbie and stuck on something that is probably real basic.

I want to get the following output from the who command:
Code:
User    TTY   Date  Time
gd22a12 pts/1 Feb 1 11:34
gd22a13 pts/3 Feb 1 11:55
gd22a14 pts/2 Feb 1 11:33

I do not want the IP addresses that come in the standard who command so I know I would need to cut somehow but Im lost between that and pacing those headers above the top of the file.

So far Ive come up with this.....
Code:
who | cut -c1-7,9-15,23-25,28-34

Problem is my headers are not lined up over the output. What would be the best way to handle this? Just a hint owuld be good and I will research the rest! :-)

Moderator's Comments:
Mod Comment Please use code tags, and a more descriptive subject title next time

Last edited by Scott; 02-06-2011 at 07:24 AM..
# 2  
Old 02-06-2011
Well, what does your who output look like? It's not the same everywhere...

For that matter, what's your system and what's your shell?
# 3  
Old 02-06-2011
Here is my batch file....
Code:
echo User TTY Date Time
who | cut -c1-7,9-15,23-25,28-34

Here is the input ...
Code:
User TTY Date Time
aar11     pts/2  Feb4 12:27
b11212d pts/1  Feb4 13:56
alf112    pts/6  Feb4 13:33

See how it doesnt line up?

Last edited by Scott; 02-06-2011 at 07:27 AM.. Reason: Code tags
# 4  
Old 02-06-2011
I meant the output of who. but whatever.

You might try this:

Code:
who | cut -c1-7,9-15,23-25,28-34 | sed 's/ /\t/g'

But then again that might not work because not all versions of sed support \t and you still haven't told me your OS and shell.

[edit] Instead try
Code:
who | cut -c1-7,9-15,23-25,28-34 | tr ' ' '\t'

I think that should be more portable.
# 5  
Old 02-06-2011
Hmmm thanks for the try but that made it worse. My OS is win7 and using the bash shell.

Using my cut the way I did it comes out fine, its the header that wont line up with the columns I cut.
# 6  
Old 02-06-2011
Quote:
Originally Posted by losingit
Hmmm thanks for the try but that made it worse.
[edit] I just realized your columns weren't made by WHO. I need to alter that echo too.

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

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-06-2011
Thats amazing....THANK YOU!!

Can you just tell me what a few things did? For instance in the first line after echo you have -e. What does -e do? I checked my text and cant find it.

Also the \t in the echo is something I havnt seen. When should this be used?

Thanks for all your help!

---------- Post updated at 08:44 AM ---------- Previous update was at 08:38 AM ----------

Oops got it, just found it on the man page!! THANKS AGAIN!!!

---------- Post updated at 10:23 AM ---------- Previous update was at 08:44 AM ----------

Ive been at this for days now and come up with two ways to pull the info out of the WHO command that I need....
Code:
who | cut -c1-7,9-15,23-25,28-34 | tr ' ' '\t'

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

I can get the first one to match up fine with the headers I made using an echo but the grep method does not align. Just out of curiousity is there a way to make my second example align?
Code:
echo -e "User\tTTY\tDate\tTime"
who | cut -c1-7,9-15,23-25,28-34 | tr ' ' '\t'
echo
echo -e "User\tTTY\tDate\tTime"
who | grep s132a | cut -d' ' -f1,3,11,13,14
echo


Last edited by Scott; 02-06-2011 at 11:37 AM.. Reason: Code tags, please...
 
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