UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 5th, 2011, 11:04 PM
losingit losingit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 5 losingit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 12 sec
Reputation Power: 0
Someone save me!!

Below is a batchfile and the output im getting. What I wanna do is create a header that says "User TTY Date and Time" all evenly spaced over their appropriate columns from the who command. As you can see its not working out. Does anyone have a minute to give me some suggestions?

echo User TTY Date Time
who | cut -c1-7,9-15,23-25,28-34

User TTY Date Time
aar11 pts/2 Feb4 12:27
b11212d pts/1 Feb4 13:56
alf112 pts/6 Feb4 13:33

Reply With Quote
  #2  
Old February 6th, 2011, 07:59 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,107 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 3 h 57 m 31 sec
Reputation Power: 1485
First off you are making some assumptions about the format of the output from the who command, it produces different output on my little Linux netbook, for example.

I'd be more inclined to lean toward using either tabs in your output (\t) and to use a field-based parser (cut -d or awk) for parsing the who output. With acknowledged differences with the who command:
Code:
$ who
ehb      tty7         2011-02-06 12:28 (:0)
ehb      pts/0        2011-02-06 12:28 (:0.0)


Code:
echo -e "User\tTTY\tDate\t\tTime"
User	TTY	Date		Time
$ who | awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'
ehb	tty7	2011-02-06	12:28
ehb	pts/0	2011-02-06	12:28
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #3  
Old February 6th, 2011, 09:18 AM
losingit losingit is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 5 losingit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 27 m 12 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
$ who | awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'
ehb tty7 2011-02-06 12:28
ehb pts/0 2011-02-06 12:28
[/CODE]


Thank you Simon, its already helping but my question is with the awk command. Ill be honest I havnt learned it yet so Im on my way to do some reading on it before I ask you questions regarding it but I wanted to at least thank you for what you gave me already!

Reply With Quote
  #4  
Old February 6th, 2011, 09:41 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,107 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 3 h 57 m 31 sec
Reputation Power: 1485
awk (not the most obvious of command names, *nix commands are usually shortened to under 8 characters or are acronyms/acrostics describing their function, awk is 'just' the initials of the original authors) is fairly simple in it's basic operation, so let's deconstruct what we are doing:

Code:
awk 'BEGIN {OFS="\t"} {print $1,$2,$3,$4}'


awk has three main 'areas' BEGIN, the 'main code' and END. BEGIN code runs once, at the start of the program, END runs once at termination of program and the main code runs once per line of input. Code is grouped together inside { }.
With tha in mind what we do is, at program start we execute the BEGIN section as we have specified one (we don't need to, but in this case it is handy) and in that all we do is {OFS="\t"} which sets an awk internal variable (OFS - Output Field Separator) to the \t (tab) character. What that will do is separate any output files with the tab character instead of the default which is a space.
Then for each line read we output $1, $2, $3, $4, using the print command, as output fields. As we have already set the OFS to be the tab character that means they get printed with tabs.
We don't need any final processing so we don't use the END option.
We could, should you wish, print the headings in the BEGIN function as well:
Code:
BEGIN {OFS="\t"; print "User","TTY","Date","","Time"}

Note the 'dummy' blank output to 'force' the extra tab to make the lining up correct. The semi-colon is the command separator, allowing multi-command lines.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Someone save me!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap