Need help to replace a perl pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to replace a perl pattern matching
# 1  
Old 04-10-2012
Need help to replace a perl pattern matching

My example file is as given below:

Code:
 conn=1 uid=oracle
conn=2 uid=db2
conn=3 uid=oracle
conn=4 uid=hash
conn=5 uid=skher
conn=6 uid=oracle
conn=7 uid=mpalkar
conn=8 uid=anarke
conn=9 uid=oracle
conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5
conn=2 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.10 to 10.18.6.5
conn=3 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.30.35.19 to 10.18.6.5
conn=4 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.40.35.11 to 10.18.6.5
conn=5 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.50.35.12 to 10.18.6.5
conn=6 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.35.14 to 10.18.6.5
conn=7 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.15 to 10.18.6.5
conn=8 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.16 to 10.18.6.5
conn=9 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.35.14 to 10.18.6.5

I need to write a scipt which will grep "uid=oracle" and find the IP address the connection is initiated from
using the connection ID "conn=x"

This is a sample file which I have kind of simplified and the actually file is in GBs.


I need to do this in perl now....

I would like an output something like this:
Code:
IP=w.x.y.z  Hits=x


Hits basically means the number of times the IP from which the seach with uid=oracle was initiated.

Any help would certainly be appreciated!

Last edited by Scrutinizer; 04-10-2012 at 07:16 AM.. Reason: Code tags in the wrong place..
# 2  
Old 04-10-2012
Try this:
Code:
#! /bin/sh

if [ $# -ne 1 ]; then
        echo "Usage: $0 <uid>"
        exit 1
fi

awk -v uid=$1 '
  NF==2 && $1~/^conn=/ && $2~/^uid=/ {
    split($1, a, "=")
    split($2, b, "=")
    conn[b[2]]=a[2] ":" conn[b[2]]
    next
  }
  /connection from/ {
    split($1, cid,"=")
    c=cid[2] ":"
    if ( match(conn[uid],c) ) {
      pos=NF-2
      ++sum[$pos]
    }
  }
  END {
    for ( i in sum ) {
      printf("IP=%s Hits=%d\n", i, sum[i])
    }
  }
' inputfile

This User Gave Thanks to chihung For This Post:
# 3  
Old 04-10-2012
Code:
$
$
$ cat f38
conn=1 uid=oracle
conn=2 uid=db2
conn=3 uid=oracle
conn=4 uid=hash
conn=5 uid=skher
conn=6 uid=oracle
conn=7 uid=mpalkar
conn=8 uid=anarke
conn=9 uid=oracle
conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5
conn=2 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.10 to 10.18.6.5
conn=3 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.30.35.19 to 10.18.6.5
conn=4 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.40.35.11 to 10.18.6.5
conn=5 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.50.35.12 to 10.18.6.5
conn=6 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.35.14 to 10.18.6.5
conn=7 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.15 to 10.18.6.5
conn=8 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.20.35.16 to 10.18.6.5
conn=9 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.35.14 to 10.18.6.5
$
$
$ perl -lane 'if ($F[1] eq "uid=oracle") {$x{$F[0]}++}
              elsif (defined $x{$F[0]}) {$y{$F[9]}++}
              END {while (($k, $v) = each %y){print "IP=$k  Hits=$v"}}
             ' f38
IP=10.10.35.14  Hits=2
IP=10.30.35.19  Hits=1
IP=10.10.5.6  Hits=1
$
$

tyler_durden
These 2 Users Gave Thanks to durden_tyler For This Post:
# 4  
Old 04-12-2012
Thanks a lot tyler_durden, is there a way to modify the perl script to include two search patterns:
Code:
 
uid=oracle and uid=db2

to get

Code:
IP=w.x.y.z  Hits=x Pattern=db2
IP=w.x.y.z  Hits=x Pattern=oracle

I figured out I can use:
Code:
 (($F[1] eq "uid=oracle") || ($F[1] eq "uid=db2"))

but can't figure out how to include Pattern=x

Last edited by sags007_99; 04-12-2012 at 03:38 AM..
# 5  
Old 04-16-2012
I would request someone's help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Replace pattern matching

Can anyone help me with sed or awk to do a bulk replace of the below requirements. "REC_ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY ( START WITH +7486 INCREMENT BY +1 MINVALUE +7467 MAXVALUE... (6 Replies)
Discussion started by: ilugopal
6 Replies

3. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

4. Shell Programming and Scripting

Perl - Use of *? in Matching Pattern

I am using Perl version 5.8.4 and trying to understand the use of regular expression. Following is my code and output. $string = "Perl is a\nScripting language"; ($start) = ($string =~ /\A(.*?) /); @lines = ($string =~ /^(.*?) /gm); print "First Word (using \\A): $start\n","Line... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

5. Shell Programming and Scripting

Pattern matching and replace in shell script

Hi I want to find a line in a file which contains a word and replace the patterns. Sample file content temp.xml ==================== <applications> <application> Name="FirstService" location="http://my.website.selected/myfirstService/V1.0/myfirst.war" ... (1 Reply)
Discussion started by: sakthi.99it
1 Replies

6. Shell Programming and Scripting

Pattern matching in Perl

Hi, I have a list of IP, eg : 192.168.0.15 192.168.0.24 192.168.2.110 192.168.2.200 And I would like the shortest pattern who match with '192.168.0' and '192.168.2' (without the last dot and number). (7 Replies)
Discussion started by: X-Or
7 Replies

7. Shell Programming and Scripting

Perl Pattern matching...

I am doing a file patterhn matching for a text file in PERL I am using this,,, but it says that no file is found $filepattern = '\d{1,4}.*A0NW9693.NDM.HBIDT.*.AD34XADJ.txt'; Can anyone help me out with Perl Pattern Matching concepts and how to do pattern matching for this txt file:... (4 Replies)
Discussion started by: msrahman
4 Replies

8. Shell Programming and Scripting

Perl pattern matching!!

Hi experts, I have many occurances of the following headers in a file. I need to grep for the word changed/inserted in the header, calculate the difference between the two numbers and list the count incrementally. Headers in a file look like this: ------------------- ---------------------... (6 Replies)
Discussion started by: nmattam
6 Replies

9. Shell Programming and Scripting

Perl Pattern Matching

Hello experts, I have a file containing the following text(shortened here). File Begin ---------- < # Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 < Billboard.d3fc1302a677.imagePath=S:\\efcm_T4 --- > # Billboard.d3fc1302a677.imagePath=S:\\efcm_Cassini >... (2 Replies)
Discussion started by: nmattam
2 Replies

10. UNIX for Dummies Questions & Answers

Pattern Matching - serach and replace script

My requirement is to replace a a particular pattren in a script from A to B. I am not sure if this can be done through sed command or through awk . The file sv.inc is window DialogBox AddConnection tag "~ActiveApp/Add Connection - Provider Type?URL" I would wnat the file to be... (10 Replies)
Discussion started by: bsandeep_80
10 Replies
Login or Register to Ask a Question