Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Merging last and syslog data on time

This is on a HP-UX system.

I need to merge the 2 reports, for each line in syslog I need to lookup who was logged in to the pts/# based on the time from the last.txt report.

Here is what I get from sulog.log

cat syslog | grep "su:" | grep "Jun 14"


Jul 14 08:02:48 server1 su: - 2 user1-root
Jul 14 09:13:23 server1 su: + 2 user1-root
Jul 14 12:03:03 server1 su: + 2 user1-root
Jul 14 18:15:13 server1 su: + 3 user2-root
Jul 14 15:03:01 server1 su: + 4 user7-root

- 2 = pts/2
+ 2 = pts/2
+ 3 = pts/3
etc....





This is from last report:

head last.txt | grep "Jul 14"
user1 pts/2 10.0.0.1 Thu Jul 14 08:00 - 10:00 (02:00)
user1 pts/2 10.0.0.2 Thu Jul 14 11:00 - 13:00 (02:00)
user2 pts/3 10.0.0.3 Wed Jul 14 16:00 - 20:00 (04:00)
user7 pts/4 hostx Wed Jul 14 13:25 - 16:01 (02:35)
.
.
.
.

So I could get:

Jul 14 08:02:48 server1 su: - 2 user1-root 10.0.0.1
Jul 14 09:13:23 server1 su: + 2 user1-root 10.0.0.1
Jul 14 12:03:03 server1 su: + 2 user1-root 10.0.0.2
Jul 14 18:15:13 server1 su: + 3 user2-root 10.0.0.3
Jul 14 15:03:01 server1 su: + 4 user7-root hostx

Any assistance would be great.

Last edited by Ikon; 07-17-2008 at 12:33 PM..
Sponsored Links
  #2 (permalink)  
Old 07-17-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,959
If you have the first output in file1 and the second output in file2:


Code:
awk '
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}
a[$7]{$0=$0 FS a[$7]}
{print}
' file2 file1

If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #3 (permalink)  
Old 07-17-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Quote:
Originally Posted by Franklin52 View Post
If you have the first output in file1 and the second output in file2:


Code:
awk '
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}
a[$7]{$0=$0 FS a[$7]}
{print}
' file2 file1

You Rock, that works great...

A couple questions...
Im still learing awk... Can you explain how this works.. What its doing so I dont have to ask about other scripts in the future and I can help others more.

really appreciate it.
  #4 (permalink)  
Old 07-17-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,959

Code:
awk '
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}
a[$7]{$0=$0 FS a[$7]}
{print}
' file2 file1

Explanation:

The code for the first file (file2):


Code:
NR==FNR{split($2,s,"/");i=s[2];a[i]=$3;next}

NR==FNR -> is true when we read the first file.
split($2,s,"/") -> we split the second field to get the keys 2, 3 etc.
i=s[2] -> i is now the key
a[i]=$3 -> create an array "a" with the key as index and assign the value of the 3th field to the array
next -> read the next line and skip the rest of the code

The code for the second file (file1):


Code:
a[$7]{$0=$0 FS a[$7]}
{print}

a[$7]{$0=$0 FS a[$7]} -> if the 7th field exists in the array append a fieldseperator and the value of the array after the line (this is the 3th field of the first file)
{print} -> print the line.

Hope this helps.

Regards
  #5 (permalink)  
Old 07-17-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
ok there is a problem, there is no check based on time.

I would need to check to see who was logged into the pts/# based on what time it was logged.

I know I can do it in perl, but would rather not.

if I have:

Jul 14 08:02:48 server1 su: - 0 user1-root
Jul 14 09:13:23 server1 su: + 0 user1-root
Jul 14 12:03:03 server1 su: + 0 user1-root
Jul 14 18:15:13 server1 su: + 0 user2-root
Jul 14 15:03:01 server1 su: + 0 user7-root


and


user1 pts/0 10.0.0.1 Thu Jul 14 08:00 - 10:00 (02:00)
user1 pts/0 10.0.0.2 Thu Jul 14 11:00 - 13:00 (02:00)
user2 pts/0 10.0.0.3 Wed Jul 14 16:00 - 20:00 (04:00)
user7 pts/0 hostx Wed Jul 14 13:25 - 15:01 (02:35)

I get:

Jul 14 08:02:48 server1 su: - 0 user1-root hostx
Jul 14 09:13:23 server1 su: + 0 user1-root hostx
Jul 14 12:03:03 server1 su: + 0 user1-root hostx
Jul 14 18:15:13 server1 su: + 0 user2-root hostx
Jul 14 15:03:01 server1 su: + 0 user7-root hostx

Last edited by Ikon; 07-17-2008 at 03:56 PM..
  #6 (permalink)  
Old 07-17-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,959
You must have one or more common fields (a key) in both files to join the files.

Regards
  #7 (permalink)  
Old 07-17-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
Quote:
Originally Posted by Franklin52 View Post
You must have one or more common fields (a key) in both files to join the files.

Regards
Common fields

Username: "userX" = "userX"-xxxxxxxx

pts: pts/"#" = - "#" userX.......

Time: ##:##:## within ##:## - ##:##

that wont be enough?
Sponsored Links
Closed Thread

Bookmarks

Tags
solaris

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UK businesses shunning real-time data analysis Linux Bot Complex Event Processing RSS News 0 07-14-2008 08:30 PM
An Introduction to Real-Time Data Integration Linux Bot Oracle Updates (RSS) 0 04-06-2008 06:10 AM
merging CSV data using a one liner from shell? jjinca Shell Programming and Scripting 2 08-13-2007 12:15 PM
Need help for 2 data file merging getdpg Shell Programming and Scripting 2 07-12-2006 10:07 AM
Merging data ReV Shell Programming and Scripting 8 06-03-2005 04:14 AM



All times are GMT -4. The time now is 05:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0