Matching using Regex inside a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching using Regex inside a file
# 1  
Old 11-17-2009
Matching using Regex inside a file

I need scan through some files, then open the file one by one and scan inside the file using perl to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag, the start tag and end tag only have 5 char. And inside the file there is "http://". It is just a if-else statement. Eg. If the file match this example, i want to print the file name out. Below is the example:

<start>jdahsjdaskjdsahttp://sanhdjashd<trats>

Anyone can help??

Last edited by blueblur; 11-17-2009 at 11:32 PM..
# 2  
Old 11-17-2009
Quote:
Originally Posted by blueblur
... open a file and scan inside ...
to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag and inside the file there is "http://"
...
And do what having seen it ?

I assume you want to print that line.
Here's a way to do it with Perl:

Code:
$
$ cat -n f9
     1  <mytag>something here</mytag>
     2  <start>jdahsjdaskjdsahttp://sanhdjashd<trats>
     3  <show>Wicked</show>
     4  A line here
     5  <author>Gregory Maguire</author>
     6  <venue>Broadwayhttp://www.broadway.com/shows/wicked<eunev>
     7  and here...
     8  <theme>Reimagining of The Wizard of Oz<emeht>
     9  <verdict>amazinghttp://amazing<tcidrev>
$
$ perl -lne 'print if /^<(.*)>.*http:\/\/.*<(.*)>/ and $2 eq reverse $1' f9
<start>jdahsjdaskjdsahttp://sanhdjashd<trats>
<venue>Broadwayhttp://www.broadway.com/shows/wicked<eunev>
<verdict>amazinghttp://amazing<tcidrev>
$
$

tyler_durden
# 3  
Old 11-17-2009
I have just amended my question. sry for the trouble. 1st time ask question here.
# 4  
Old 11-25-2009
'print if /^<(.*)>.*http:\/\/.*<(.*)>/ and $2 eq reverse $1'

How about i want 186 char in "*http:\/\/" portion?
# 5  
Old 11-26-2009
Quote:
Originally Posted by blueblur
'print if /^<(.*)>.*http:\/\/.*<(.*)>/ and $2 eq reverse $1'

How about i want 186 char in "*http:\/\/" portion?
I don't think I understood your question. In the perl one-liner posted earlier:

Code:
.*http:\/\/.*

checks for the text between the tags. The regex matches the text if -

(1) there are 0 or more characters right after the ">" character of the opening tag
(2) followed by the characters "http://"
(3) followed by 0 or more characters till the "<" character of the closing tag.

How do 186 characters fit into this picture ?
- 186 characters before "http://" ? After "http://" ? Including "http://" ?
- Also, <= 186 characters ? Exactly 186 ? >= 186 ?

tyler_durden
# 6  
Old 11-26-2009
Quote:
Originally Posted by durden_tyler
How do 186 characters fit into this picture ?
- 186 characters before "http://" ? After "http://" ? Including "http://" ?
- Also, <= 186 characters ? Exactly 186 ? >= 186 ?
tyler_durden

-Exactly 186 char including "http://"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regex matching column awk

Hi all, I want to extract rows with the pattern ALPHANUMERIC/ALPHANUMNERIC in the 2nd column. I dont wan rows with more than 1 slash or without any slash in 2nd column. a a/b b a/b/c c a/b//c d t/y e r f /f I came up with the regex grep '\/$' file a a/b b a/b/c d t/y (3 Replies)
Discussion started by: jianp83
3 Replies

2. UNIX for Dummies Questions & Answers

Regex matching with grep -l

I am trying to find patterns in files using grep -l -e. I specifically am searching for abc. I want any file that has abc in it, but not just the letters abc. I am searching for a pattern a followed by b followed by c. I have tried egrep -l and also I have tried the following: grep -el... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

4. Shell Programming and Scripting

matching a regex using egrep not working

Hi, I'm trying to validate if a string matches a regular expression, but it is not working. Am I missing something? Do I need to scape any of the characters? if echo 'en-GB' | egrep '({1,8})(-{1,8})*' >/dev/null; then echo Valid value fi Thanks in advance (6 Replies)
Discussion started by: skrtxao
6 Replies

5. Shell Programming and Scripting

Help with matching pattern inside a file

I have a huge file that has roughly 30304 lines. I need to extract specific info from that file. For example, Box 1 > *aaaaaaaajjjj* > hbbvjvj > jdnnfddllll > *dgdfhfekwjh* Box 2 > *aaaaaaa'aj'jjj* > dse hkjuejef bfdw > dyeee > dsewq > *dgdfhfekwjh* >feweiuei Box 3 > *aaaa"aaaaj"jjj* >... (25 Replies)
Discussion started by: Ernst
25 Replies

6. Shell Programming and Scripting

Perl: Regex, string matching

Hi, I've a logfile which i need to parse and get the logs depending upon the user input. here, i'm providing an option to enter the string which can be matched with the log entries. e.g. one of the logfile entry reads like this - $str = " mpgw(BLUESOAPFramework):... (6 Replies)
Discussion started by: butterfly20
6 Replies

7. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

8. Shell Programming and Scripting

Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses? For example, if the string is "((foo bar))", I want to be able to say "match any number of... (7 Replies)
Discussion started by: cvp
7 Replies

9. Shell Programming and Scripting

regex inside if comparison

I'm trying to compare the last octet of an IP to a regex: IP=$(ifconfig eth0 | grep inet | awk -F: '{print $2}' | awk -F. '{print $4}' | awk '{print $1}') if ]; then echo "GOOD: Correct IP range for server" else echo "ERROR:... (6 Replies)
Discussion started by: s_becker
6 Replies

10. Shell Programming and Scripting

REGEX: Matching Null?

I'm using the URL Regex feature of Squid for allowing sites via a list of regex strings to match allowed domains. The regex was actually copied from our previous proxy solution and it seemed to "just work". But, we've recently discovered that some domains (likely due to virtual hosts or host... (2 Replies)
Discussion started by: deckard
2 Replies
Login or Register to Ask a Question