LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 11:06 AM   #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, 11:09 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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, 11:36 AM   #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, 12:39 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 01: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, 04:51 PM   #6
robertdaleweir
Member
 
Registered: Jul 2006
Location: Canada
Distribution: Fedora
Posts: 93

Rep: Reputation: 11
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, 05: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, 06:45 PM   #8
robertdaleweir
Member
 
Registered: Jul 2006
Location: Canada
Distribution: Fedora
Posts: 93

Rep: Reputation: 11
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, 07: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, 02:46 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What do you require? I have not seen another question and you seemed to have what you needed??
 
Old 10-02-2015, 08: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, 08:48 AM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 10:19 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:37 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
Open Source Consulting | Domain Registration