How to remove "\n" character

  • Follow


Good morning
I have a file that contains data like this:
data1
dat2
dat3
dat4
.....

I want to create a file like this:
data1 data2 data3 data4 ....

How to do this using awk? The filed separator must be the space
character.
Thanks for your help.
0
Reply mazouz.nezhate (88) 5/26/2008 9:35:29 AM

On Monday 26 May 2008 11:35, Nezhate wrote:

> Good morning
> I have a file that contains data like this:
> data1
> dat2
> dat3
> dat4
> ....
> 
> I want to create a file like this:
> data1 data2 data3 data4 ....
> 
> How to do this using awk? The filed separator must be the space
> character.
> Thanks for your help.

awk -v ORS=' ' 1 yourfile

or

awk -v ORS=' ' '1;END{printf"\n"}' yourfile

if you want the trailing newline. Note that the above solutions add a trailing
blank after the last field.

-- 
All the commands are tested with bash and GNU tools, so they may use nonstandard
features. I try to mention when something is nonstandard (if I'm aware of
that), but I may miss something. Corrections are welcome.
0
Reply pk 5/26/2008 9:41:46 AM


On Monday 26 May 2008 11:35, Nezhate wrote:

> Good morning
> I have a file that contains data like this:
> data1
> dat2
> dat3
> dat4
> ....
> 
> I want to create a file like this:
> data1 data2 data3 data4 ....
> 
> How to do this using awk? The filed separator must be the space
> character.
> Thanks for your help.

One way could be (assuming your input has no empty lines):

awk -v RS= '$1=$1' file

This however reads the whole file in a single step, so you may have problems if
the file is very large.

-- 
D.
0
Reply Dave 5/26/2008 10:58:53 AM

Thanks for your Help :-)
0
Reply Nezhate 5/26/2008 11:45:02 AM

On Monday 26 May 2008 13:45, Nezhate wrote:

> Thanks for your Help :-)

Note also that my solution uses the default FS and will compact runs of
consecutive blanks in the input lines (I forgot to mention that before, sorry).
You probably did not have that problem, however, to avoid that, you can add 

-v FS=',' 

to the command line (where ',' is any character that does not appear in the
input).

-- 
D.
0
Reply Dave 5/26/2008 11:55:42 AM

In article <g1e11f$lve$1@registered.motzarella.org>, pk  <pk@pk.invalid> wrote:
>On Monday 26 May 2008 11:35, Nezhate wrote:
>
>> Good morning
>> I have a file that contains data like this:
>> data1
>> dat2
>> dat3
>> dat4
>> ....
>> 
>> I want to create a file like this:
>> data1 data2 data3 data4 ....
>> 
>> How to do this using awk? The filed separator must be the space
>> character.
>> Thanks for your help.
>
>awk -v ORS=' ' 1 yourfile
>
>or
>
>awk -v ORS=' ' '1;END{printf"\n"}' yourfile

ITYM:

awk -v ORS=' ' '/t[0-9]/ {sub(/dat/,"data")}1;END{printf"\n"}' yourfile

>-- 
>All the commands are tested with bash and GNU tools, so they may use
>nonstandard features. I try to mention when something is nonstandard
>(if I'm aware of that), but I may miss something. Corrections are
>welcome.

It looks to me like you didn't test it at all.

0
Reply gazelle 5/26/2008 2:18:53 PM

On Monday 26 May 2008 16:18, Kenny McCormack wrote:

>>awk -v ORS=' ' '1;END{printf"\n"}' yourfile
> 
> ITYM:
> 
> awk -v ORS=' ' '/t[0-9]/ {sub(/dat/,"data")}1;END{printf"\n"}' yourfile

ITYM

awk -v ORS=' ' '/t[2-4]$/ {sub(/t/,"ta")}1;END{printf"\n"}' yourfile

Ok, that's already too much troll-feeding for today.

>>All the commands are tested with bash and GNU tools, so they may use
>>nonstandard features. I try to mention when something is nonstandard
>>(if I'm aware of that), but I may miss something. Corrections are
>>welcome.
> 
> It looks to me like you didn't test it at all.

Or the OP made some typos while describing his data. Let's see if he wants to
clear the issue for us.

-- 
All the commands are tested with bash and GNU tools, so they may use nonstandard
features. I try to mention when something is nonstandard (if I'm aware of
that), but I may miss something. Corrections are welcome.
0
Reply pk 5/26/2008 2:23:23 PM

6 Replies
276 Views

(page loaded in 0.06 seconds)

Similiar Articles:













7/26/2012 9:34:50 AM


Reply: