Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-27-2003, 10:54 AM
|
#1
|
LQ Newbie
Registered: Jul 2003
Posts: 8
Rep:
|
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
Last edited by brokenfeet; 08-27-2003 at 10:57 AM.
|
|
|
08-27-2003, 10:55 AM
|
#2
|
LQ Newbie
Registered: Jul 2003
Posts: 8
Original Poster
Rep:
|
sorry...
@file = FILE;
should read
@file =<FILE>;
paul
Last edited by brokenfeet; 08-27-2003 at 10:58 AM.
|
|
|
08-27-2003, 12:23 PM
|
#3
|
Member
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415
Rep:
|
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
Last edited by sk8guitar; 08-27-2003 at 12:25 PM.
|
|
|
08-27-2003, 01:52 PM
|
#4
|
LQ Newbie
Registered: Jul 2003
Posts: 8
Original Poster
Rep:
|
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
|
|
|
|
08-28-2003, 04:12 PM
|
#5
|
Member
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415
Rep:
|
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
|
|
|
08-28-2003, 04:16 PM
|
#6
|
Moderator
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047
Rep:
|
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.
|
|
|
08-28-2003, 04:23 PM
|
#7
|
LQ Newbie
Registered: Jul 2003
Posts: 8
Original Poster
Rep:
|
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
|
|
|
08-29-2003, 01:56 PM
|
#8
|
Member
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223
Rep:
|
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 04:16 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|