Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Matching 3

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
Hi all!

I have a file which separates each record with a empty newline at the end. I'm reading this file and pattern matching with characters i need to capture. But my problem is I'm not able to capture the empty newline at the end of the each record.

here is the file


record1
sasdsdasd
sadasdasd
asdasdadasd

record2
ers: ererklejr
erer:djfhsdf
dkjfkdf: djfksdfj
jskjfsdf: sdkfjksdfj

Record3
dfdfj: sdfjdkf
dfskfj: dfkjskf

currently my output looks something like this



record1|record2

i wanted the output to be

record1
record2

I'm not sure how to match the empty newline as every line has a newline ..i'm not sure how to differentiate a empty new line and a line with newline at the end.
here is what my newline match code looks

if($output=~/\n/)
{
print "At the end of the record \n";
}

but this matches with every single line of the file as it has a newline at the end.

Any advise will be greatly appreciated.

Thanks.




 
Do you mean an empty line that only has a newline on the end?

/^\s*$/




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I've been away for a while and separated from perl, but:

It sounds like what you need to do is to set $INPUT_RECORD_SEPERATOR (more commonly referred to as $/ ) to be "\n\n".

You then want up to and including the first newline of each record.

Proof of concept code:
Code:
$/="\n\n";
print substr($_,0,index($_,"\n")+1) foreach( <DATA> );

__DATA__
Record1
r1l1
r1l2
r1l3

Record2
r2l1
r2l2
r2l3

Record3
r3l1
r3l2
r3l3
 
Kevin, Yes empty line only has new line at the end.
 
OK, can you show use your code or have you tried Brigmars suggestion in regards to $/?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It's coming back to me now: if you modify $/, you should scope it only to when you need it, otherwise it will affect subsequent file operations.

{
local $/ = "\n\n";
# do file operations here
}

 
To match an empty line you should use ^$ really, like this.

if($output=~/^$/)

^$ ignores the \n (newline character or characters) at the end of the line.

^$ won't match blank lines that contain spaces though, to do that you can use ^\s*$

Mike

 
Thanks Mike,Brigmar & kevin. All your valuable comments worked. Finally i went with mike's suggestion

if($output=~/^$/)


Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top