CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
perl regular expression cannot be matched?

 



notorious
Novice

Aug 8, 2011, 4:27 PM

Post #1 of 5 (1566 views)
perl regular expression cannot be matched? Can't Post

Hi,

I am trying to run an expression to match a particular line of text in a file, then capture the entire rest of the file using a perl one-liner:

For example, in the following file, I want to match starting at Header2 (ie the first header after ".X-Intermail-Unknown-MIME-Type=unparsedmessage" to the end of the file.

<lsmothers@example.com>
SMTP
0<001501c4db9b$db8b2680$2d01a8c0@ryand9v889t9uc>
.X-Intermail-Unknown-MIME-Type=unparsedmessage
Header2: <headertwo@example.com
Received: from server.cluster1.example.com ([10.20.201.160])
line 12


EVERYTHING I have done around this does not catch it. An example of what I have tried is below. From what I read, I think the below method should work:

$ cat sample.txt | perl -wnl -e '/\.X\-Intermail\-Unknown\-MIME\-Type\=unparsedmessag(e.*)/s and print $1;'
e

The above returns only the e character, and I did that on purpose to make sure the string is matched. I am using the s modifier after the expression, which would categorize "newline" as "any".

Can anyone give me any suggestions?

Thanks, Robert


FishMonger
Veteran / Moderator

Aug 8, 2011, 5:32 PM

Post #2 of 5 (1563 views)
Re: [notorious] perl regular expression cannot be matched? [In reply to] Can't Post

Since you're reading the data line-by-line, using the s option on the regex won't do anything useful. You would need to read in the data in chunks, not line-by-line.

Please don't cat the file and pipe it, pass the file as an arg.
Useless Use of cat http://partmaps.org/era/unix/award.html


(This post was edited by FishMonger on Aug 8, 2011, 5:33 PM)


notorious
Novice

Aug 9, 2011, 2:37 PM

Post #3 of 5 (1547 views)
Re: [FishMonger] perl regular expression cannot be matched? [In reply to] Can't Post

ooops - I did the same thing a few months ago....it was cat...ill have to write a short script and open the file properly.

Thanks


notorious
Novice

Aug 9, 2011, 2:41 PM

Post #4 of 5 (1546 views)
Re: [notorious] perl regular expression cannot be matched? [In reply to] Can't Post

Nice - I got the award LOL!


BillKSmith
Veteran

Aug 10, 2011, 4:50 AM

Post #5 of 5 (1528 views)
Re: [notorious] perl regular expression cannot be matched? [In reply to] Can't Post

It is not necessary to open the file explicitly. You are using the perl runtime option -n. Refer to perldoc perlrun to see that this option reads the file using the input operator with a null file handle "<>". In perldoc perlop, we find that the input operator with a null file handle is a special case. Like sed and awk, it can open the files specified as arguments.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives