LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   trying to search and replace text file for single & multiple line breaks separately (http://www.linuxquestions.org/questions/programming-9/trying-to-search-and-replace-text-file-for-single-and-multiple-line-breaks-separately-86276/)

brokenfeet 08-27-2003 11:54 AM

trying to search and replace text file for single & multiple line breaks separately
 
In this case, a user updates the text file using a webform, and may want to include double AND single line breaks as a means of simple formatting.

I'm trying to determine how to discern between the two when reading the file for display on the website (using a PERL script).

Right now, I'm using the following code:

open (FILE, $filename);
@file = <FILE>;
close FILE;

foreach $line (@file) {
$line =~ s/\n/<BR>/g;
print "$line";

Obviously, I can replace <BR> with <P> to force a double line break, but it's not always relavent.

I've tried the following to pick up the double line break, but they didn't work:

$line =~ s/\n\n/<P>/g;
$line =~ s/\n+/<P>/g;


Any suggestions?

Thanks,
Paul

brokenfeet 08-27-2003 11:55 AM

sorry...

@file = FILE;

should read

@file =<FILE>;

paul

sk8guitar 08-27-2003 01:23 PM

wouldn't 2 br's give you the same as a p tag? is it really that necessary? cause i just started to think about it and if you wanted to sometimes use a <br> and sometimes use a <p> you'd have to look ahead and behind a couple lines and see how many spaces there are. for example, if there was only 1 \n then you do a <br>, but if there are 2 \n you do a <p>. you can't just write s/\n\n/<p>/ because the \n aren't ont he same lines. they are on seperate lines, so that instance never happens.

you'd have to do something like
Code:

if($line[now_element]=~/\n/ && $line[next_element]=~/\n/ && $line[next_element]!~/\w+/)
# the line you are on has a \n, the next line has a \n and no words (a blank space)
{
  $line[next_element]=~s/\n//; #replace the next lines empty space with nothing, basically deleting it
  $line[now_element]=~s/\n/<p>/; #replace the current lines \n with a <p> tag
}

or something along those lines

brokenfeet 08-27-2003 02:52 PM

I don't mind using <BR> at the end of one line and another <BR> on the new blank line where the second /n resides, but it doesn't seem to work that way -- it's not updating with two breaks. That would actually be my preferred method.

I guess I'll keep trying....

Paul



Quote:

Originally posted by sk8guitar
wouldn't 2 br's give you the same as a p tag? is it really that necessary? ...
you can't just write s/\n\n/<p>/ because the \n aren't ont he same lines. they are on seperate lines, so that instance never happens.

you'd have to do something like
Code:

if($line[now_element]=~/\n/ && $line[next_element]=~/\n/ && $line[next_element]!~/\w+/)
# the line you are on has a \n, the next line has a \n and no words (a blank space)
{
  $line[next_element]=~s/\n//; #replace the next lines empty space with nothing, basically deleting it
  $line[now_element]=~s/\n/<p>/; #replace the current lines \n with a <p> tag
}

or something along those lines


sk8guitar 08-28-2003 05:12 PM

can you show a sample statment of what we are trying to convert with br tags? i found one time that doing the browser added weird "characters" to the end of each line that i couldn't use a reg exp to match and get rid of. (like ^M). it should work if you just want to convert every \n to a <br> in the way you originally posted, so i'm thinking that its not a problem with the code but with the file

david_ross 08-28-2003 05:16 PM

Try removing carrige returns first - ie:
$line =~ s/\r//eg;

Then replace newlines with line breaks:
$line =~ s/\n/<BR>/eg;

Using <p> is bad since you will never close the tag.

brokenfeet 08-28-2003 05:23 PM

resolution
 
i resolved my problem as follows, but i may try some suggestions and see what else i come up with.

foreach $line (@bio) {
if (($line =~ /\w/) && ($line =~ /\n/)) {
$line = $line . "<BR>";
}
elsif ($line =! /\w/) {
$line = "<P>";
}
}


paul

SaTaN 08-29-2003 02:56 PM

A much more easier way of doing this is to replace the default delimiter for a file which is "\n" to "^D" . Thus all the file will be placed in a single variable n u can use the regular expression on this variable...
The code is

$/="^D";
open(FILE,"text");
$str=<FILE>;
$str=~s/\n/<br>/g;
print $str;

The "^D" is got by using (CTRL+v ) and then (CTRL + d)...It is not normal "carrot + capital D" but the end of file specification....If u have any problem reply....


All times are GMT -5. The time now is 06:41 PM.