How can I replace newline character?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How can I replace newline character?
# 1  
Old 08-16-2001
How can I replace newline character?


I am trying to write a script to prepare some text for use as web content.

What is happening is that all the newlines in the textfile are ignored, so I want
to be able to replace/add a few characters so that for a file containg:
This is line 1.
This is line two.

This is line four.
And this line is very long and covers more than two lines of text 
so there is no need to break it up in any way.
Another short line.

is is reformatted to become:
This is line 1.&ltbr&gt
This is line two.&ltbr&gt
This is line four.&ltbr&gt
And this line is very long and covers more than two lines of text 
so there is no need to break it up in any way.&ltbr&gt
Another short line.&ltbr&gt

I have tried to use : tr "\n" "\n&ltbr&gt" but it will only replace with same number or fewer characters.

I tried using sed as well but was unable to find a way to search
for the newline character, I tried:

sed -e 's/\n/\n&ltbr&gt/g'
sed -e 's/^M/^M&ltbr&gt/g'
sed -e 's/\^M/\^M&ltbr&gt/g'

but they either do not work, or end up splitting the script line apart due to being taken as newline characters in the script. Is there any way to do this?


PS, if it helps any, I am using a BASH shell on Redhat Linux 7.0

Last edited by ghoti; 08-16-2001 at 01:09 PM..
# 2  
Old 08-16-2001
below 2 code examples.

1) just add a &ltbr&gt at the end of EACH line that has a hard return @ the end.

2) add a &ltBR&gt at every "." followed by a hard return.

1) sed 's/$/\<BR\>/' test.html
2) sed 's/\.$/\BR\>/' test.html

also note. if you want a new line in sed its a 2 line script. you have to use a ^J to get it in there.


if you wanted to use tr use the octal form of a hard return. \12 i think.

Last edited by Optimus_P; 08-16-2001 at 02:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

3. Shell Programming and Scripting

Scripting for PuttyCM (NewLine Character)

Hi, I am trying to edit a script to create an XML file to upload into PuttyCM. Everything works with the exception of the NewLine character. By default this option is set to Line feed (LF). I need it to be set to NewLine (CR/LF) <options> <loginmacro>True</loginmacro> ... (4 Replies)
Discussion started by: Jett44
4 Replies

4. Shell Programming and Scripting

Replace newline character between a double quotes to a space

Hi Guys, I have a file with content as below aj.txt "Iam allfine" abcdef abcd "all is not well" What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text. output must be:... (8 Replies)
Discussion started by: ajahuja
8 Replies

5. Shell Programming and Scripting

replacing by newline character

I have a file (pema)with a single long record which i have to break up into multiple lines Input s1aaaaaaaaaaaaaaaaaaaaaaas1bbbbbbbbbbs1cccccccccc Output s1aaaaaaaaaaaaaaaaaaaaaaa s1bbbbbbbbbb s1cccccccccc m planning to do it by replacing s1 by \ns1 \n is the new line character i... (5 Replies)
Discussion started by: pema.yozer
5 Replies

6. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

7. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

8. Shell Programming and Scripting

Why SED can't see the last newline character?

Removed. My question does not make sense. and SED does see the last newline character. But I still have a question: How to remove the last newline character(the newline character at the end of last line) using SED? ---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was... (7 Replies)
Discussion started by: kevintse
7 Replies

9. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

10. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies
Login or Register to Ask a Question