LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-17-2010, 08:36 AM   #1
samji9999
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Rep: Reputation: 0
awk to sum up login time of a user on linux from output of cmd "last -a"


output of "last -a" is as follows:
usr pts/3 Tue Aug 17 12:28 - 12:44 (00:15) dom1.broadband.za
usr pts/3 Tue Aug 17 11:06 - 11:25 (00:18) dom1.broadband.za

I need to sum up $9, which is 15+18, but the parenthesis come in way and sum comes zero.

last -a|awk '{sum=sum+$9} {print sum}'

What am I doing wrong?

Thanks in advance.
 
Old 08-17-2010, 09:02 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,541

Rep: Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919Reputation: 1919
Well as you quite rightly pointed out, the brackets and also the zeroes and : are getting in the way.
Remove all of these and then remember it will be a unit of 60 in the end that you require unless you only want minutes.

I would also urge you to consider when you will print the total??
 
Old 08-17-2010, 09:11 AM   #3
scottn
LQ Newbie
 
Registered: Jul 2010
Posts: 14

Rep: Reputation: 2
Hello.

If your awk supports multiple field-separators, then you could try:

Code:
last -a | awk -F"[(:)]" '{Total+=$4*60+$5} END {print Total}'
 
Old 08-17-2010, 08:53 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by scottn View Post
Hello.

If your awk supports multiple field-separators, then you could try:

Code:
last -a | awk -F"[(:)]" '{Total+=$4*60+$5} END {print Total}'
just minor note, sometimes the column may be 2+15:45 for example.
 
Old 08-17-2010, 10:06 PM   #5
ejinh
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
Guys, Good Day.

any tips from you on how to filter my awk output?
I want to exclude last 5 characters using awk in my tcpdump result.

ex.
192.168.34.12.443:

I don't want to include ".443:" in my tcpdump using awk.

Thanks a ton.
 
Old 08-17-2010, 10:26 PM   #6
joec@home
Member
 
Registered: Sep 2009
Location: Houston Tx
Posts: 89

Rep: Reputation: 29
Quote:
Originally Posted by ejinh View Post
Guys, Good Day.

any tips from you on how to filter my awk output?
I want to exclude last 5 characters using awk in my tcpdump result.

ex.
192.168.34.12.443:

I don't want to include ".443:" in my tcpdump using awk.

Thanks a ton.
Code:
echo 192.168.34.12.443 |cut -d. -f1,2,3,4
192.168.34.12
 
Old 08-17-2010, 11:09 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by joec@home View Post
Code:
echo 192.168.34.12.443 |cut -d. -f1,2,3,4
192.168.34.12
a tcpdump output is has more info than that.
 
Old 08-17-2010, 11:10 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by ejinh View Post
Guys, Good Day.

any tips from you on how to filter my awk output?
I want to exclude last 5 characters using awk in my tcpdump result.

ex.
192.168.34.12.443:

I don't want to include ".443:" in my tcpdump using awk.

Thanks a ton.
your tcpdump should have more info right? provide a more solid example and your desired output
 
Old 08-17-2010, 11:26 PM   #9
ejinh
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
thanks, this how i get my tcpdump result,

tcpdump -n -q -t -i pflog0 port 443

output:

192.168.77.101.51218 > 65.54.51.29.443: tcp 0 (DF)
192.168.77.101.51234 > 65.54.51.29.443: tcp 0 (DF)
192.168.77.101.51235 > 65.54.51.29.443: tcp 0 (DF)
192.168.77.101.53454 > 65.55.184.16.443: tcp 0 (DF)
192.168.77.101.53460 > 65.55.184.16.443: tcp 0 (DF)

in my column $3 i want to exclude .443: using awk.

thanks.
 
Old 08-17-2010, 11:30 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk '{sub(/\..[^.]*:/,"",$3);}1' file
you should start a new thread since this is a tcpdump question

Last edited by ghostdog74; 08-18-2010 at 01:24 AM.
 
Old 08-17-2010, 11:46 PM   #11
ejinh
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
ghostdog74, thanks for the prompt reply i really appreciate your help. yes i have new thread,
subject: awk: how to exclude last several characters

thanks a ton.
 
Old 08-15-2014, 12:54 PM   #12
PeterVermont
LQ Newbie
 
Registered: Aug 2014
Posts: 2

Rep: Reputation: Disabled
version to get total time from format hh:mm:ss

Code:
awk -F"[(:)]" '{h+=$1; m+=$2; s+=$3} END{m+=int(s/60); h+=int(m/60); m=m%60; s=s%60; printf("%d:%02d:%02d\n", h, m, s)}' input_file.txt
I left this showing hours but if one wanted to go one step further to calculate days:

Code:
awk -F"[(:)]" '{h+=$1; m+=$2; s+=$3} END{m+=int(s/60); h+=int(m/60); d=int(h/24); h=h%24; m=m%60; s=s%60; printf("%d:%02d:%02d:%02d\n", d, h, m, s)}'
 
  


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
square brackets in output of "ps aux" not matching output of "ps -ejH" alirezan1 Linux - Newbie 14 07-14-2010 04:17 AM
automatic mail of "Last User Login Logs" & "AIX Logs" nishith AIX 3 12-04-2009 12:42 PM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
Why is the output says " this file has anunknown MD5 sum cc281828748e6fc9da3c68897321 judepeomichael Linux - Wireless Networking 2 06-27-2009 08:51 AM
CSH: "cmd >& file" or "cmd </dev/null >& file" stefanlasiewski Programming 1 09-08-2003 04:19 PM


All times are GMT -5. The time now is 05:26 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration