You are not logged in.

#1 2005-04-07 20:33:50

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

removing newlines from a file?

ok, I want to remove all newlines from a file... I don't think sed will work, as it reads line by line.... does anyone know a way to do this?

FYI, I have an xml file, some 8000 lines long, which has some bad newlines just due to the sheer size... as in:

<someNode a="a" b="b" something="som<NEWLINE>
ething"/>

and I want it to go away 8(

Offline

#2 2005-04-07 21:15:42

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: removing newlines from a file?

why does nobody like tr?
echo -e "n" | tr -d 'n'

Offline

#3 2005-04-07 21:24:35

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: removing newlines from a file?

wow, rock on!
there's little utilities like that that I totally forget about all the time...

kinda like "modinfo"

Offline

#4 2005-04-07 21:35:59

i3839
Member
Registered: 2004-02-04
Posts: 1,185

Re: removing newlines from a file?

Took me way too long to discover "tee"...

Offline

#5 2005-04-07 22:30:29

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: removing newlines from a file?

in python there is replace() method, like...

file = open("path/to/file", "r")
string = file.read()
file.close()
string_new = string.replace("n", "")
file = open("/path/to/file_new", "w")
file.write(string_new)
file.close()

that should fix it,


arch + gentoo + initng + python = enlisy

Offline

#6 2005-04-07 22:37:56

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: removing newlines from a file?

I tried that in .NET, actually, and for some reason it wasn't working.... the basic process was:
call a stored procedure
convert massive results to XML
stream XML to file

real simple... however String.Replace wasn't doing crap for the newlines... I dunno why...but it always had embedded newlines at the worst places...

seeing as this is one of those apps where ya go "aha something changed, time to regenerate the XML file" every month or so, it wasn't important *how* it's done... so I was just going to pipe it to tr (found a windows version).

On a side note, I did get the procedure down from taking ~7mins to run to under a minute... hooray for optimization!

Offline

#7 2005-04-07 23:32:32

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

Re: removing newlines from a file?

phrakture wrote:

I don't think sed will work, as it reads line by line.... does anyone know a way to do this?

It's possible but tr is much easier. See 5.10 from
http://www.student.northpark.edu/pement … dfaq5.html

Offline

#8 2005-04-08 19:02:14

klapmuetz
Member
From: Germany
Registered: 2005-03-19
Posts: 75

Re: removing newlines from a file?

vim -c '%s/n//' <file>

If I got the question right. :-)

BR,
klapmuetz


Hello girls, I like rock climbing, mountain biking and rafting! Write me!

Offline

#9 2005-04-11 17:43:50

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: removing newlines from a file?

btw. why is this topic not in the programming forum (I needed to search for it).

Offline

#10 2005-04-11 17:46:54

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: removing newlines from a file?

cmp wrote:

btw. why is this topic not in the programming forum (I needed to search for it).

good question - I should move it

Offline

Board footer

Powered by FluxBB