trying to search and replace text file for single & multiple line breaks separately
ProgrammingThis 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.
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.
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
}
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
}
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
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
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....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.