LinuxQuestions.org
Review your favorite Linux distribution.
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 05-05-2009, 05:59 AM   #1
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Rep: Reputation: 0
Question Selecting certain parts of a list of columns in BASH


Hi all,

I have got a list:

Code:
...USER 1  11:18  Fri May 1 10.0.0.1
USER2  11:17  Fri May 1 10.0.0.25
USER2  05:42  Fri May 1 10.0.0.85...
This shows when each user last logged in, and from what IP address.

I would like to show all of the users that logged in outside of office hours (ie, users that have logged in between 18:00 - 08:00).

I have been able to filter out just the time, using awk then an if statement, however then i lose all the details about the username and ipaddress etc.

Can anyone advise me of a better way to do this? (or indeed how to code properly so this way will work!!)

Thanks in advance
 
Old 05-05-2009, 06:18 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Please, can you post the code you've tried so far? Do you want to extract the whole lines, right?
 
Old 05-05-2009, 06:25 AM   #3
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Original Poster
Rep: Reputation: 0
Yes im trying to extract the full line..

Im looking through my history to find the code ive tried so far.

I used:
Code:
last | sed '$d' | sed '$d' | awk '{print $1,$7,$4,$5,$6}' | cut -d" " -f2 | cut -d":" -f1
Which cuts out the hour, and i assigned to a variable so i could put in an IF statement to check if it was >18 or <08, then however I cant get the rest of the info back around it.

Cant find other things that I've tried
 
Old 05-05-2009, 06:52 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can extract the hour and use an if statement inside the awk program itself:
Code:
last | head -n -2 | awk '{ hour = gensub(/:.*/,"","g",$7); if ( hour+0 < 8 || hour+0 >= 18) print $1,$7,$4,$5,$6}'
the hour+0 trick is to force awk to consider hour as numeric, otherwise numbers with leading zeros are treated as string and cannot be used in arithmetic comparison. Please note that some lines in the output of the last command are different from the major part and this simple awk program does not manage them as well.

Last edited by colucix; 05-05-2009 at 06:57 AM. Reason: corrected awk program
 
Old 05-05-2009, 07:53 AM   #5
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Original Poster
Rep: Reputation: 0
Hi,

Thanks, that does work, and now just formatting it to something reasonable .

Can you please explain what the hell:
Code:
 gensub(/:.*/,"","g",$7);
means??

Cheers
 
Old 05-05-2009, 09:17 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by mikejreading View Post
Can you please explain what the hell:
Code:
 gensub(/:.*/,"","g",$7);
means??
gensub is a substitution function. Since the seventh field is something like 11:34, we want to remove the :34 part, or better we can substitute the :34 with nothing. The syntax is
Code:
gensub(regexp, replacement, how [, target])
The first argument is a regular expression that must match the part you want to substitute, that is a colon and the characters that follow:
Code:
/:.*/
where the slashes are the delimiters for the regular expression, the colon is literal and the .* means any number (zero included) of any character.
The second argument is the replacement, that is an empty string since we want to completely remove that part of the string. The third argument can be "g" or "G" to substitute all matches of regexp, otherwise it is a number that indicate which match of regexp to replace. The last argument is the target, that is the input string. If omitted the full line ($0) is considered as target.

For more detailed explanations, take a look at the very good Gawk user's guide.
 
Old 05-07-2009, 05:48 AM   #7
mikejreading
LQ Newbie
 
Registered: May 2009
Posts: 24

Original Poster
Rep: Reputation: 0
Thanks for that colucix.

Thanks for all your help :-)

Mike
 
  


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
run-parts gives error on bash script ChoKamir Programming 9 04-11-2017 08:18 AM
sorting columns in bash twistadias Linux - Newbie 8 08-25-2008 01:22 AM
Bash - How can you list 2 arrays in columns zak111 Programming 12 07-23-2007 12:27 PM
Parts list for new system build, Linux compatible? zephyrcat Linux - Hardware 1 06-07-2007 06:20 PM
bash thinks term is 80 columns? JurgyMan Linux - General 4 06-13-2006 10:14 AM

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

All times are GMT -5. The time now is 07:54 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