replace space for enter

i have to print in a html file directories like this

/home/user
/home/user/dir

but the problem is that when i us this comand

listado=`find $direcreal -type f -print`

i get this

/home/user /home/user/dir1

i try with sed to replace the space with an enter

mostrarlistado=`echo "$listado" | sed "s/\ /\n/g"`

but i keep printing the same result

You'll have to quote it correctly. Compare the output of

echo $mostrarlistado

to

echo "$mostrarlistado"

Try it with tr instead of sed:

tr " " "\n"

with tr " " "\n" i get the same result
/home/user /home/user/dir1

Maybe your problem is in the actual HTML? If you're using the results straight from sed, you're probably getting something like this:

<div id="blablabla">
/home/user
/home/user/dir
</div>

And if that's the case, you're missing the <br /> tag that signals a line break in HTML. If I remember correctly, implicit line breaks like the ones above turn into spaces when HTML is rendered on to a webpage.

So, if this is your problem, try replacing the spaces with "<br />" instead of newlines.

It is like Pludi says, just do a:

echo "$listado"

All right, now I got it!

find $direcreal -type f -print

This already prints file names separated/delimited by "\n", but then you assign this output to a variable, where AFAIK all "\n" get converted to a whitespace. Later you actually do the same with another variable.
Why not simply redirect the output of the find command to a file?

Hi pseudocoder. Not exactly. It is the other way around. When you assign the output to a variable, the newline characters are retained. If you later echo that variable without quotation they are converted to spaces. If you echo the variable with quotation the newline characters are not converted to spaces.

1 Like

why not try something like:

or something of the kind. The exact formatting is not critical, but that it be correct html or xhtml to display properly when written to the html page and rendered in a browser does!

Check into other options, like wrapping it in

tags as another option.

(Note, you can load the variable as you did, then parse and format it on output. This was just one example.)

I tried everything you all said but nothing works

---------- Post updated at 07:38 PM ---------- Previous update was at 05:21 PM ----------

Yes ! finally i find the solution
prueba=`echo "$mostrarlista" | cut -d" " -f1`
<pre>$prueba</pre>