LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-01-2015, 12:06 PM   #1
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Rep: Reputation: Disabled
last command


Hello Experts,

Am trying to use last -w command to get the users logged in to the machine,It is working fine.But my requirement is to get only fews fields to the output file like username,hostname and Date to the output file,Can someone help on this ?

Regards,
 
Old 10-01-2015, 12:09 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by ahmed.vaghar View Post
Hello Experts,
Am trying to use last -w command to get the users logged in to the machine,It is working fine.But my requirement is to get only fews fields to the output file like username,hostname and Date to the output file,Can someone help on this ?
Simplest way would be to pipe it into awk, and print out what you need. Like:
Code:
last -w | awk '{print $1","$2}'
 
1 members found this post helpful.
Old 10-01-2015, 12:36 PM   #3
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thumbs up

Awesome.

Thank you TB0ne really you have given a break.

Can you also please help me how to write the output in csv or excel file ? Am trying the below command it is giving syntax error.

last -w| awk 'printf "%-15s\t%-15s\%-15s\t%-15s\n", "USERNAME", "LOGGEDIN_FROM", "LOGIN_DATE","LOGIN_TIME","LOGIN_DUARATION"}' '{print $1","$3","$5","$6","$7","$8","$9","$10}'

Thank you very much for your help.

Regards
 
Old 10-01-2015, 01:39 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I would urge some caution and to check your results as the date component, and maybe others, is not a single field
 
Old 10-01-2015, 02:22 PM   #5
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Grail,

I agree with you,

Below the command.

last -w -F | awk 'printf "%-15s\t%-15s\%-15s\t%-15s\n", "USERNAME", "HOSTNAME","LOGIN_TIME", "LOGOUT_TIME","LOGIN_DUARATION"}' '{print $1","$3","$5$6$7$8","$11$12$13$14","$15}'
 
Old 10-01-2015, 05:51 PM   #6
robertdaleweir
Member
 
Registered: Jul 2006
Location: Canada
Distribution: Fedora
Posts: 70

Rep: Reputation: 8
Quote:
Originally Posted by ahmed.vaghar View Post
Grail,

I agree with you,

Below the command.

last -w -F | awk 'printf "%-15s\t%-15s\%-15s\t%-15s\n", "USERNAME", "HOSTNAME","LOGIN_TIME", "LOGOUT_TIME","LOGIN_DUARATION"}' '{print $1","$3","$5$6$7$8","$11$12$13$14","$15}'
Hi ahmed.vaghar
Your awk function seems to throw errors when I run it.
 
Old 10-01-2015, 06:01 PM   #7
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Can you try this ?

last -w -F | awk '{print "USERNAME" $1","$3","$5$6$7$8","$11$12$13$14","$15}' | tac | tr ',' '\t'
 
Old 10-01-2015, 07:45 PM   #8
robertdaleweir
Member
 
Registered: Jul 2006
Location: Canada
Distribution: Fedora
Posts: 70

Rep: Reputation: 8
Quote:
Originally Posted by ahmed.vaghar View Post
Can you try this ?

last -w -F | awk '{print "USERNAME" $1","$3","$5$6$7$8","$11$12$13$14","$15}' | tac | tr ',' '\t'
Hi ahmed.vaghar
Works fine, thank you.
 
Old 10-01-2015, 08:51 PM   #9
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
Can you help me out here ?
 
Old 10-02-2015, 03:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
What do you require? I have not seen another question and you seemed to have what you needed??
 
Old 10-02-2015, 09:43 AM   #11
ahmed.vaghar
LQ Newbie
 
Registered: Jul 2015
Posts: 10

Original Poster
Rep: Reputation: Disabled
I need to write this output in csv or excel file with the column names.

Please help me
 
Old 10-02-2015, 09:48 AM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Ok.

Code:
echo "user,ip,begin,end,total"
last -w -F | awk '{print $1","$3","$5$6$7$8","$11 $12 $13 $14","$15}' | tac
 
Old 10-02-2015, 11:19 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,919

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by ahmed.vaghar View Post
I need to write this output in csv or excel file with the column names. Please help me
We did help you...a better approach for you would be to take the hints/advice you've been given, and LEARN from them. You could easily think about what you were given, and have figured out how to get a CSV file on your own.

Please try to show some effort of your own.
 
  


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
Any Linux command output to delimited format. Script should work for any command. torrelm@hotmail.com Linux - Newbie 9 09-04-2014 09:54 AM
Command/Script required to send an email alert in case command will not respond rajaniyer123 Linux - General 1 05-19-2012 02:12 PM
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 04:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 12:21 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 03:14 AM


All times are GMT -5. The time now is 02:05 PM.

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