How to remove newlines in text file 
Author Message
 How to remove newlines in text file

Dear awkers,

I want to remove the newlines (or replace them by blank spaces) in a
text file. I'm not a user of sed or awk. I read the online manual of
sed. but found the simple command failed:

sed 's/\n/ /' textfile

It seems that sed can only process text file line by line, so it can not
remove the newlines.
Can anyone tell me how to do it using sed or awk?

Thanks!

Jie Zhang



Sun, 19 Dec 2004 10:34:26 GMT  
 How to remove newlines in text file

Quote:
> Dear awkers,

> I want to remove the newlines (or replace them by blank spaces) in a
> text file. I'm not a user of sed or awk. I read the online manual of
> sed. but found the simple command failed:

> sed 's/\n/ /' textfile

> It seems that sed can only process text file line by line, so it can not
> remove the newlines.
> Can anyone tell me how to do it using sed or awk?

> Thanks!

> Jie Zhang

Check "man tr"

In awk, try
    awk '{ printf "%s", (NR==1?"":" ") $0 }' data > data.new; rm data; mv
data.new data
(The awk works, not sure about your shell.)

Also, try
    awk 'BEGIN{ORS=" "}1' data > data.new; rm data; mv data.new data
"1" is an expression that evaluates to true, which invokes the patter, which
is NULL, and thus taken as the default "{print}", which, lacking argument,
by default, takes $0.  In other words...
    awk "BEGIN{ORS=\" \"}{print $0}"
This adds a space to the end of your file.

    - Dan

But this is a filter...you need to deal with creating a new file



Sun, 19 Dec 2004 12:11:19 GMT  
 How to remove newlines in text file

Quote:

> Dear awkers,

> I want to remove the newlines (or replace them by blank spaces) in a
> text file. I'm not a user of sed or awk. I read the online manual of
> sed. but found the simple command failed:

> sed 's/\n/ /' textfile

> It seems that sed can only process text file line by line, so it can not
> remove the newlines.
> Can anyone tell me how to do it using sed or awk?

> Thanks!

> Jie Zhang

paste -s
tr '\n'

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin



Sun, 19 Dec 2004 12:20:08 GMT  
 How to remove newlines in text file

Quote:

> Dear awkers,

> I want to remove the newlines (or replace them by blank spaces) in a
> text file. I'm not a user of sed or awk. I read the online manual of
> sed. but found the simple command failed:

> sed 's/\n/ /' textfile

> It seems that sed can only process text file line by line, so it can not
> remove the newlines.
> Can anyone tell me how to do it using sed or awk?

> Thanks!

> Jie Zhang

paste -s
tr -d '\n'

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin



Sun, 19 Dec 2004 12:21:37 GMT  
 How to remove newlines in text file

Quote:

> paste -s
> tr -d '\n'

It works. Many thanks!

Jie Zhang



Sun, 19 Dec 2004 13:29:12 GMT  
 How to remove newlines in text file

Quote:
> I want to remove the newlines (or replace them by blank spaces) in a
> text file. I'm not a user of sed or awk. I read the online manual of
> sed. but found the simple command failed:
> sed 's/\n/ /' textfile
> It seems that sed can only process text file line by line, so it can not
> remove the newlines.
> Can anyone tell me how to do it using sed or awk?

It isn't sed or awk, but tr seems to be the right tool

tr -d '\012' <textfile; or
tr '\012'  ' ' <textfile.

        mike

--

Any opinions  expressed are  mine and not necessarily
those of any other entity. They may not even be mine.



Sat, 25 Dec 2004 03:28:35 GMT  
 How to remove newlines in text file
I want to do a similar thing. I tried using tr but it seemed to only
output the last line of the file. I figure this is because it is the
only line that doesnt have a new line character in it.
Any ideas on how I could turn my 1000 line file into a single line
file would be appreciated.
Thanks
Quote:


> > I want to remove the newlines (or replace them by blank spaces) in a
> > text file. I'm not a user of sed or awk. I read the online manual of
> > sed. but found the simple command failed:

> > sed 's/\n/ /' textfile

> > It seems that sed can only process text file line by line, so it can not
> > remove the newlines.
> > Can anyone tell me how to do it using sed or awk?

> It isn't sed or awk, but tr seems to be the right tool

> tr -d '\012' <textfile; or
> tr '\012'  ' ' <textfile.

>    mike



Tue, 11 Jan 2005 23:33:51 GMT  
 How to remove newlines in text file
Have you tried the earlier-posted
    awk 'BEGIN{ORS=" "}1' data > data.new; rm data; mv data.new data
If you don't like the spaces where line breaks used to be, try ORS="".
    - Dan


Quote:
> I want to do a similar thing. I tried using tr but it seemed to only
> output the last line of the file. I figure this is because it is the
> only line that doesnt have a new line character in it.
> Any ideas on how I could turn my 1000 line file into a single line
> file would be appreciated.
> Thanks




Quote:

> > > I want to remove the newlines (or replace them by blank spaces) in a
> > > text file. I'm not a user of sed or awk. I read the online manual of
> > > sed. but found the simple command failed:

> > > sed 's/\n/ /' textfile

> > > It seems that sed can only process text file line by line, so it can
not
> > > remove the newlines.
> > > Can anyone tell me how to do it using sed or awk?

> > It isn't sed or awk, but tr seems to be the right tool

> > tr -d '\012' <textfile; or
> > tr '\012'  ' ' <textfile.

> > mike



Tue, 11 Jan 2005 23:52:09 GMT  
 How to remove newlines in text file
On 26 Jul 2002 08:33:51 -0700,

Quote:
>I want to do a similar thing. I tried using tr but it seemed to only
>output the last line of the file. I figure this is because it is the
>only line that doesnt have a new line character in it.
>Any ideas on how I could turn my 1000 line file into a single line
>file would be appreciated.
>Thanks

If your file has lines ending with crlf, and you remove the lf characters,
it could look as if you only got the last line of the file, because each
line is written on top of the previous one.
tr -d '\015\012'


Wed, 12 Jan 2005 06:44:29 GMT  
 How to remove newlines in text file
Thanks for that!
Quote:

> On 26 Jul 2002 08:33:51 -0700,

> >I want to do a similar thing. I tried using tr but it seemed to only
> >output the last line of the file. I figure this is because it is the
> >only line that doesnt have a new line character in it.
> >Any ideas on how I could turn my 1000 line file into a single line
> >file would be appreciated.
> >Thanks

> If your file has lines ending with crlf, and you remove the lf characters,
> it could look as if you only got the last line of the file, because each
> line is written on top of the previous one.
> tr -d '\015\012'



Fri, 14 Jan 2005 21:18:59 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Remove trailing newlines from file

2. Modifying existing script to remove trailing newlines

3. Removing final newline from a Tcl string using C API

4. Howto remove text from a file with awk

5. REMOVE LAST CHARACTER OF TEXT FILE

6. remove double spacing from text file

7. PEP 259: Omit printing newline after newline

8. PEP 259: Omit printing newline after newline

9. Windows-newlines in text-widgets

10. $canvas find enclosed does not find text with trailing newline

11. getting text widget to show newlines and tabs as \n and \t

12. How do i remove file loaded combo from file schematic of window

 

 
Powered by phpBB® Forum Software