Perl Pattern Match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Pattern Match
# 1  
Old 11-18-2009
Perl Pattern Match

Hi Friends, I have a tuff time with regular expressionss. Please let me know how to make this happen as it consumed lots of my time but in vain. Here is the sample text file i need to match for. I need to search for pattern1 removed, if it matches then search for pattern types either SE\ or dcfm-derby-dataload.sql not both. Similarly i need to search for pattern2 added and if it matches then search for either dcm-postgres-schema.sql or migrate\. Here i need to print files(dcm-postgres-schema.sql) and directories(migrate\) separately if it matches for removed and if it matches for added separately. Please help me.

---------[ removed ]---------------|----------------------------------
SE\ 2008-11-01 vbhupati |-
---------[ removed ]---------------|----------------------------------
dcfm-derby-dataload.sql vo+|-
---------[ removed ]---------------|----------------------------------
dcfm-derby-schema.sql voba+|-
---------[ removed ]---------------|----------------------------------
dcfm-nms-sybase-dataload.sql 27T+|-
-----------------------------------|---------------[ added ]----------
-| dcm-inm-postgres-schema.sql T0+
-----------------------------------|---------------[ added ]-----------
-| dcm-postgres-dataload.sql 23T08:+
----------------------------------|---------------[ added ]-----------
-| dcm-postgres-schema.sql -T01:58+
-----------------------------------|---------------[ added ]-----------
-| migrate\ --10-13T06:31 ycho

Output must be like, i should be able to print both the lines:
---------[ removed ]---------------|----------------------------------
SE\ 2008-11-01 vbhupati |-
---------[ removed ]---------------|----------------------------------
dcfm-derby-dataload.sql vo+|-
.
.
.
-----------------------------------|---------------[ added ]----------
-| dcm-inm-postgres-schema.sql T0+
-----------------------------------|---------------[ added ]-----------
-| dcm-postgres-dataload.sql 23T08:+
.
.
.
Please help me. Thanks, nmattam
# 2  
Old 11-18-2009
Hi.

If you have access to command glark, you can do this from the command line. Here is a short example of glark on your data:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate complex matching using "glark".

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) glark
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results with \"removed\":"
glark -U -a 1 "removed" -o 'SE\\' "dcfm-derby-dataload.sql" $FILE

echo
echo " Results with \"added\":"
glark -U -a 1 "added" -o 'migrate\\' "dcm-postgres-schema.sql" $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
glark, version 1.8.0

 Data file data1:
---------[ removed ]---------------|----------------------------------
SE\ 2008-11-01 vbhupati |-
---------[ removed ]---------------|----------------------------------
dcfm-derby-dataload.sql vo+|-
---------[ removed ]---------------|----------------------------------
dcfm-derby-schema.sql voba+|-
---------[ removed ]---------------|----------------------------------
dcfm-nms-sybase-dataload.sql 27T+|-
-----------------------------------|---------------[ added ]----------
-| dcm-inm-postgres-schema.sql T0+
-----------------------------------|---------------[ added ]-----------
-| dcm-postgres-dataload.sql 23T08:+
----------------------------------|---------------[ added ]-----------
-| dcm-postgres-schema.sql -T01:58+
-----------------------------------|---------------[ added ]-----------
-| migrate\ --10-13T06:31 ycho

 Results with "removed":
    1 ---------[ removed ]---------------|----------------------------------
    2 SE\ 2008-11-01 vbhupati |-
    3 ---------[ removed ]---------------|----------------------------------
    4 dcfm-derby-dataload.sql vo+|-
    5 ---------[ removed ]---------------|----------------------------------

 Results with "added":
   13 ----------------------------------|---------------[ added ]-----------
   14 -| dcm-postgres-schema.sql -T01:58+
   15 -----------------------------------|---------------[ added ]-----------
   16 -| migrate\ --10-13T06:31 ycho

Briefly, this says that matches must occur within one line of each other. The -a means "and", the -o means "or". Line 5 is printed because it is within one line of matched line number 4.

The two cases are separated here to avoid (some) confusion, but I think a master pattern could be created to handle it all in one pass over the data file. It would, however, not be easy to read. The glark code is written in ruby.

I installed it from the Debian (5, "lenny") repository. More information can be found at glark and glark | freshmeat.net

I don't see this as complicated from the viewpoint of regular expressions, but it is complex from the viewpoint of structuring decisions.

If you don't have access to glark, then you will probably need to use perl or awk. There are many experts on both here, so someone may be along shortly to help with that ... cheers, drl

-----

Busy server seemed to post this response twice ... cheers, drl

Last edited by drl; 11-18-2009 at 11:51 AM..
# 3  
Old 11-19-2009
Tools Perl Pattern Match

Dear drl,
I appreciate your help, but i do not have access to glark. I appreciate if any one can help me with another possible solution.
Thanks, nmattam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

2. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

3. Shell Programming and Scripting

PERL - Use of uninitialized value in pattern match (m//)

I have written a PERL script to read files from directory that the filename contains OT. It then takes each line of each file and prints the first 5 characters before the first occurence of a /. Currently I am getting the error: Use of uninitialized value in string at rowGrab.pl line 43.... (3 Replies)
Discussion started by: chris01010
3 Replies

4. Shell Programming and Scripting

Perl match pattern

Hi all, i have a peice of Perl script like this: foreach (@line) { @tmp = split /;/,$_; #print "Line is: $_\n"; switch($tmp){ case m/p60/i { push @p60, , $tmp ]; ... (7 Replies)
Discussion started by: arrals_vl
7 Replies

5. Shell Programming and Scripting

How to replace with pattern match using Perl

I have to replace a line, if it has a pattern for example Suppose file.out contains: <tr><td class="cB">Hello</td><td class="cB">1245</td><td class="cB">958</td><td class="cRB">1.34</td><td class="cRB">1.36</td></tr> <tr><td class="cB">world</td><td class="cB">3256</td><td... (8 Replies)
Discussion started by: sol_nov
8 Replies

6. Shell Programming and Scripting

perl pattern match on xml

using perl Hi All, i was wondering if anyone can solve how to extract the full tag from the xml line ie not sure what to put in the m// to get the string "/data/TOP471//context_data/instruments.txt" I basically want the above filename in a variable for further processing... $_ =" ... (0 Replies)
Discussion started by: satnamx
0 Replies

7. Shell Programming and Scripting

Perl Array / pattern match large CPU usage

Hi, I have one file in this format 20 value1 33 value2 56 value3 I have another file in this format: 34,30-SEP-09,57,100237775,33614510126,2,34 34,30-SEP-09,57,100237775,33620766654,2,34 34,30-SEP-09,108,100237775,33628458122,2,34 34,30-SEP-09,130,100237775,33635266741,2,254... (6 Replies)
Discussion started by: Donkey25
6 Replies

8. Shell Programming and Scripting

pattern match url in string / PERL

Am trying to remove urls from text strings in PERL. I have the following but it does not seem to work: $remarks =~ s/www\.\s+\.com//gi; In English, I want to look for www. then I want to delete the www. and everything after it until I hit a space (but not including the space). It's not... (2 Replies)
Discussion started by: mrealty
2 Replies

9. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

10. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies
Login or Register to Ask a Question