LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 08-27-2003, 10:54 AM   #1
brokenfeet
LQ Newbie
 
Registered: Jul 2003
Posts: 8

Rep: Reputation: 0
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.
 
Old 08-27-2003, 10:55 AM   #2
brokenfeet
LQ Newbie
 
Registered: Jul 2003
Posts: 8

Original Poster
Rep: Reputation: 0
sorry...

@file = FILE;

should read

@file =<FILE>;

paul

Last edited by brokenfeet; 08-27-2003 at 10:58 AM.
 
Old 08-27-2003, 12:23 PM   #3
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
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.
 
Old 08-27-2003, 01:52 PM   #4
brokenfeet
LQ Newbie
 
Registered: Jul 2003
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 08-28-2003, 04:12 PM   #5
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
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
 
Old 08-28-2003, 04:16 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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.
 
Old 08-28-2003, 04:23 PM   #7
brokenfeet
LQ Newbie
 
Registered: Jul 2003
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 08-29-2003, 01:56 PM   #8
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Search and Replace with multiple-line strings ChristianNerds.com Programming 4 08-21-2005 02:32 PM
Looking for a multiple file search and replace tool, prefer graphical haimeltjnfg Linux - Software 6 02-02-2005 10:53 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM


All times are GMT -5. The time now is 08:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration