Identifying entries based on 2 fields in a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identifying entries based on 2 fields in a string.
# 1  
Old 02-25-2012
Identifying entries based on 2 fields in a string.

Hi Guys,

I’m struggling to use two fields to do a duplicate/ unique by output.

I want to look IP addresses assigned to more than one account during a given period in the logs. So duplicate IP and account > 1 then print all the logs for that IP. I have been Using AWK (just as its installed on the system in question). Any help would be greatly appreciated.

log:
Code:
2012/01/01:01:30:35  type: 1, ip-assigned: 10.10.10.236, account: E8C200511C63, 
2012/01/01:01:30:36  type: 2, ip-assigned: 10.10.10.236, account: E8C200511C63, 
2012/01/01:01:30:37  type: 1, ip-assigned: 10.10.10.37, account: E8C3004BF14E,
2012/01/01:01:30:38  type: 2, ip-assigned: 10.10.10.11, account: E8C201111C63,
2012/01/01:01:30:39  type: 1, ip-assigned: 10.10.10.228, account: E8C300314D4A, 
2012/01/01:01:30:40  type: 2, ip-assigned: 10.10.10.147, account: E8C30031407A, 
2012/01/01:01:30:41  type: 1, ip-assigned: 10.10.10.236, account: E8C3003149CA, 
2012/01/01:01:30:42  type: 2, ip-assigned: 10.10.10.37, account: E8C3004BF14E, 
2012/01/01:01:30:43  type: 1, ip-assigned: 10.10.10.37, account: E8C3007069AD,
2012/01/01:01:30:44  type: 1, ip-assigned: 10.10.10.11, account: E8C201DB1C63,
2012/01/01:01:30:45  type: 2, ip-assigned: 10.10.10.228, account: E8C300314D4A, 
2012/01/01:01:30:46  type: 1, ip-assigned: 10.10.10.230, account: E8C300314D4A, 
2012/01/01:01:30:47  type: 2, ip-assigned: 10.10.10.230, account: E8C300314D4A,
2012/01/01:01:30:48  type: 1, ip-assigned: 10.10.10.101, account: E8C200511C63,

Desired output;
Code:
2012/01/01:01:30:35  type: 1, ip-assigned: 10.10.10.236, account: E8C200511C63, 
2012/01/01:01:30:36  type: 2, ip-assigned: 10.10.10.236, account: E8C200511C63, 
2012/01/01:01:30:41  type: 1, ip-assigned: 10.10.10.236, account: E8C3003149CA, 
2012/01/01:01:30:37  type: 1, ip-assigned: 10.10.10.37, account: E8C3004BF14E,
2012/01/01:01:30:42  type: 2, ip-assigned: 10.10.10.37, account: E8C3004BF14E, 
2012/01/01:01:30:43  type: 1, ip-assigned: 10.10.10.37, account: E8C3007069AD,
2012/01/01:01:30:38  type: 2, ip-assigned: 10.10.10.11, account: E8C201111C63,
2012/01/01:01:30:44  type: 1, ip-assigned: 10.10.10.11, account: E8C201DB1C63,


Last edited by Franklin52; 02-26-2012 at 05:13 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-25-2012
Do you have Perl installed?
Code:
perl -ane '$h{$F[4]}{$F[6]}=1;$s{$F[4]}.=$_;END{for $i (keys %h){@acc=keys %{$h{$i}};print $s{$i} if $#acc>0}}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-25-2012
BASH

Hi,

Try this one,

Code:
#! /usr/bin/bash
 
logfile="LogFile"
outfile="OutFile"
tmpfile="tmpfile"
 
cut -d',' -f2,3 $logfile | sort -u >$tmpfile

for i in `cut -d',' -f1 $tmpfile | sort -u | sed 's/ ip-assigned: //'`
do
   cnt=`grep -c $i $tmpfile`
   if [ $cnt -gt 1 ];then
      grep $i $logfile >>$outfile
   fi
done

Configure your log file name and output file name based on your needs.

Cheers,
Ranga Smilie

Last edited by rangarasan; 02-25-2012 at 10:59 AM..
# 4  
Old 02-25-2012
Thanks

bartus11

Many thanks, that worked a treat!

Wabbit02!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records File2 has 2 fields. I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched) File2: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

3. Shell Programming and Scripting

Identifying columns and their values based on schema file

I have 3 files, data file,schema file and a threshold file. Data file contains data in which columns are distributed according to schema file. This data file doesn't contain any headers. Three continuous columns in the data file represent single variable in schema file. first column represent... (1 Reply)
Discussion started by: bharathbangalor
1 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Awk - Script assistance on identifying non matching fields

Hoping for some assistance. my source file consists of: os, ip, username win7, 123.56.78, john win7, 123.56.78, paul win7, 10.1.1.1, john win7, 10.2.2.3, joe I've been trying to run a script that will only return ip and username where the IP address is the same and the username is... (3 Replies)
Discussion started by: tekvaio
3 Replies

6. Shell Programming and Scripting

Help identifying the first word in a string

Hi all, I'd like to know how to identify the first word in a string (in bash) for e.g. echo "enter your name" read name (user enters 'Joe Bloggs' for e.g.) echo "hello $name" (output says "hello Joe") Thanks for any help (5 Replies)
Discussion started by: kazazza
5 Replies

7. Shell Programming and Scripting

Identifying specific fields in a Row

Hi, I am new to UNIX. Can some one help me to solve the below. I have a requirement to to identify the specific fields in row and also some part of the field. In my file I have a record as sundra;10.44.48.65;10thstreet TCP packet out of state: First packet isn't SYN;telno:... (3 Replies)
Discussion started by: suneel.mekala
3 Replies

8. Shell Programming and Scripting

Identifying a string from a set of files and printing to a new file

Dear All, I'm an amateur to writing scripts and need to do the following Need to read all files with a .log extension in a directory and identify the value for username i.e. all files have something like username = John. Once this is read, I need to print this value to a new file. The new file... (2 Replies)
Discussion started by: Kelly_B
2 Replies

9. Solaris

Identifying new fields of data

i have hundreds of lines of formatted data with 10 different fields per line. the data is refreshed every few minutes and some fields in some lines may reflect new data. i'm looking for a sample of code that help me to identify those new fields so that i can write them to a file to indicate that... (0 Replies)
Discussion started by: davels
0 Replies
Login or Register to Ask a Question